locked
Web API 2.0 "405 method not allowed" frustration RRS feed

  • Question

  • User-1693238866 posted

    Hi guys.

    I'm experiencing the wierdest thing I've ever seen.

    I have a simplest possible Web API 2.0 service. Here's its code:

    WebApiConfig class contents:

    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "GuestActivityApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
        config.Filters.Add(new ChainAuthorizeAttribute());
    }
    
    public class ChainAuthorizeAttribute : AuthorizeAttribute
    {
    	protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
    	{
    		string bscAuth = System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(actionContext.Request.Headers.Authorization.Parameter));
    		string un = bscAuth.Split(':')[0];
    		string up = bscAuth.Split(':')[1];
    		// I omit code where I check if credentials are OK and fetch 'cc' from DB and return FALSE if credentials are wrong
    		HttpContext.Current.Items.Add("CC", cc);
    		
    		return true;		
    	}
    }

    And the only controller I have looks like this (I've cleared all logs comments etc.):

    public class SignalController : ApiController
    {
    	public long Post(GuestHeader gh)
    	{
    		HCEntities hce = new HCEntities();
    		CC cc = (CC)HttpContext.Current.Items["CC"];
    
    		try
    		{
    			hce.SetGuestActivity(null, gh.Id, gh.EventId, cc.ID.ToString(), gh.Token);
    		}
    		catch
    		{
    			return 0;
    		}
    
    		return 1;
    	}
    }

    This code works fine on my local PC and under some domain (e.g. www.mydomain.com).

    I mean when I run Postman to check it I use this URL to post JSON to it: http://www.mydomain.com/SignalAPP/API/signal (SignalAPP is an application under www.mydomain.com) and it runs as smoothely as can be....

    Now, due to my company's new paradigm I have to move the code under subdomain signalapp.mydomain.com so the url for POSTs would be http://signalapp.mydomain.com/API/signal 

    And here the frustration starts.....

    I copy the code from folder refered by www.mydomain.com/SignalAPP/ and paste it to a folder refered by signalapp.mydomain.com and when I post to http://signalapp.mydomain.com/API/signal I get 405 - method not allowed..... Ta-da!!!

    And something even wierder - when I post to http://signalapp.mydomain.com/API/signal/blah ("blah" can be replaced with any random set of characters) it does work....

    The question is - what pills should I take to set myself at ease 'cause this situation drives me crazy?Cry

    Some additional points:

    • Both the domain and its subdomain run under the same IIS server (8.5) and refer to folders that seat one next another on the same harddrive, same partition, and have the same set of permissions as far as I can tell.
    • Same for the application pools (I even tried to put the subdomain under domain's application pool - useless)
    • I don't have WebDAV installed so, please don't direct me to articles about how to disable it.
    • I've tried virtually all the "solutions" I could have found on the Internet and of course I've searched this forum for "405" and opened every single post and tried every single suggestion - to no avail...

    And finally a little song to encourage you to help me on this one:

    I need a guru!
    I'm holding out for a guru till the issue's resolved!
    Guru's got to be strong
    Guru's got to code fast
    Guru's got to have my problem solved!
    I need a guru!
    I'm holding out for a guru till the bug's out of sight!
    Guru's got to be sure
    And it's got to be soon
    Guru's got to be larger than life

    See how crazy I get because of this issue?

    Thursday, October 29, 2015 4:05 AM

All replies

  • User901658981 posted

    You could first disable the old sub application and then restart the IIS to try again.  In most cases, the 405 error is related to the WebDAV handler, as you said you don’t install it, you could refer to this article to check it again. Besides, the following similar thread that provide a method to solve it and you could have a look.

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/60631-WebApi-405-not-allowed-when-called-via-Ajax#comment-205574

    Friday, October 30, 2015 5:14 AM
  • User901658981 posted

    You could first disable the old sub application and then restart the IIS to try again.  In most cases, the 405 error is related to the WebDAV handler, as you said you don’t install it, you could refer to this article to check it again. Besides, you could have a look at the following similar thread that provides a solution for it.
    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/60631-WebApi-405-not-allowed-when-called-via-Ajax#comment-205574

    Friday, October 30, 2015 5:15 AM
  • User-1693238866 posted

    Hi G.AprilHist,

    Thank you for your reply.

    As I said I don't have WebDAV isntalled at all. By that I mean that I don't even have that "WebDAV Authoring Rules" icon. It's just don't exists on my server. The feature is not installed. At all.

    Also I've seen the Umbraco forum article and it didn't help.

    Anyway the issue is sloved by now.
    I have no idea how but after a series of recompiling the same code (with adding logs but not changing the main functionality) and resetting the IIS server it started working as it should.
    I ran several tests of sending tens of thousand fake records (in both serial and parallel ways) to the API and everything went smoothly.
    So... Still frustrated but at least colmed down.... :-)

    Sunday, November 8, 2015 10:48 AM