Azure Mobile App - Getting 405 (Method Not Allowed) when trying POST - c#

I'm trying to migrate my Azure Mobile Service .NET backend to an Azure Mobile App.
I was using some custom Web Api controllers, and after migration I'm getting a 405 (Method Not Allowed) / The requested resource does not support http method 'POST'. error when trying to POST to a controller method that worked before.
I spent hours trying diffent CORS settings but I had no success so far.
This is how I currently configure Web Api:
HttpConfiguration config = new HttpConfiguration();
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
var cors = new EnableCorsAttribute("*", "*","*");
//var cors = new EnableCorsAttribute("*", "*","GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "Rest",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapHttpAttributeRoutes();
The controller looks like that:
[Authorize]
[RoutePrefixAttribute("rest/companies")]
public class CompaniesController : ApiController
{
[HttpPost]
[Route("my-active")]
//[EnableCors("*","*","*")]
public HttpResponseMessage SetActive(/*[FromBody]*/Company company)
{
// Implementation
}
}
What I tried too:
Set CORS settings in web.config (custom headers / different settings), eg. <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS" />
Added a cors message handler according this blog post
(http://blog.bittercoder.com/2012/09/09/cors-and-webapi/)
This handler is also removed: <remove name="OPTIONSVerbHandler" />
One thing I noticed is, that a Azure Mobile App component seems to override the allowed methods and allowed headers that I configured using config.EnableCors(cors). I was only able to control all settings using web.config and the message handler. But it did not solve the 405 problem anyway.
At this point, I'm not sure if it's a CORS problem at all.
Any ideas? It's currently hard to find good documentation on Mobile Apps and I would appreciate if the .NET backend part would be open sourced... It's somewhat of a black box for me.

It could happen when you activate App Service Authorization and forget to change your mobile client url from http to https.
If so, your http Post will be redirected to the https url but with a Get message.
Found it thanks to Fiddler.

OMG, I found the problem with my code. I had to swap this two statements:
// Needs to be called before MapHttpRoute
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Rest",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
As I was using Azure Mobile Services, calling MapHttpAttributeRoutes caused an error 'An item with the same key has already been added', so I removed that line. I had to re-insert it for Azure Mobile Apps again in order to get attribute routing to work, but I did it at the wrong place, so be careful.

If http Post is redirected to the https url as Get, try calling https directly.
Azure logs looks as follows in this case:
Received request: POST http://xxx.azurewebsites.net/api/Data/test
Information Redirecting: https://xxx.azurewebsites.net/api/Data/test
Received request: GET https://xxx.azurewebsites.net/api/Data/test
in this case call https://xxx.azurewebsites.net/api/Data/test

Related

Simple controller routing doesn't work in IIS for asp.net site hosted in IIS

Trying to host locally a rest service in IIS. Everything works fine in IIS Express but as soon as I try to host locally in IIS the routing doesn't work.
It's compiled to x86 and is hosted inside of an app pool that allows for 32 bit processes.
I have the site mapped to the binary output folder from my build msbuild /p:Configuration=Release /p:ProcessorArchitecture=x86 -r:False
I have Anonymous Authentication enabled
Binding is type http to port 8000
I have enabled directory browsing and see the below at http://localhost:8000/
However whenever I try to navigate to http://localhost:8000/api/HeartBeat I get a 404 - Not Found.
My WebApiConfig class which sets a default route.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Here is the controller
public class HeartBeatController : ApiController
{
// GET: api/HearBeat/5
public HttpResponseMessage Get()
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
If I run this in IIS Express and navigate to that same url I get a 200. Also I've tried to examine the URL in powershell using Get-WebUrl and it shows a ProtocolError but I have no clue what or why. And IIS Manager doesn't seem to complain about anything.
PS C:\Windows\system32> Get-WebURL -PSPath "IIS:\Sites\Test" | Format-list
Status : ProtocolError
ResponseUri : http://localhost:8000/
Description : Forbidden
A couple of things were amiss.
Publishing via Visual Studio is quite different than the build output that was created via the build command above.
I was missing under Windows features/IIS/Application Development Features
Finally was still getting a security error which required adding an ACL rule to the directory

Unable to use MVC Controller with ASP.Net Web API 2

What I have so far (that works)
I have an ASP.Net Web API 2 project. Well, at least that's what I remember creating when I set up the project, I am not sure how to confirm that.
I am using Visual Studio 2017, and .Net Framework version 4.6.
In terms of the API side of things, this is all working great. The API controllers are fine, I can get data, post data, etc.
Just a bit of additional information in case it matters, I have added SignalR to the project which has been configured.
As it may be important, here are my various configuration files:
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
What I am trying to do (that doesn't work)
However, I want to add a HTML page so the user can view some information (just static HTML stuff, nothing special). So I have created a standard MVC controller with an action like so (and the Index.cshtml view is in the correct Views folder):
public class NotificationsController : Controller
{
public ActionResult Index()
{
return View();
}
}
The problem is that this action never gets run (I have a breakpoint).
What I have tried to identify the problem
Now I get at this point, it could be loads of different things, so here is what I have tried so far to debug the issue:
When I access the URL in a browser (e.g. http://localhost:59461/Notifications), I get:
localhost is currently unable to handle this request.
HTTP ERROR 500
At first I thought maybe this is a routing issue, however in VS 2017 you can see that a request has failed for this action:
So surely the routing must be working correctly? Unfortunately, clicking the requests only confirms the 500 error and doesn't give any more information about the problem.
The only additional information I can find is in the Windows Event Viewer, in which I get the following error:
Application 'MACHINE/WEBROOT/APPHOST/PROJECTNAME' with physical root 'C:\PATH TO PROJECT FOLDER\' failed to start process with commandline '%LAUNCHER_PATH% %LAUNCHER_ARGS%', ErrorCode = '0x80070002 : 0.
But I have researched that error a lot and am yet to find a suitable solution or explanation for my problem.
I have also tried adding Application_Error but that isn't throwing any exceptions either.
At this point I don't know how to work out the cause of the problem. The only thing I can think of is that I need to configure something specifically to allow Web API projects to work with MVC controllers, but I can't find anything on that either.
What can I do to debug this problem correctly, and find the cause?
Urgh... so I solved the problem...
After stumbling across this post, there is a suggestion to delete the .vs folder in the Visual Studio solution folder. After doing this, and rebuilding the solution, it started working.
No idea what is in that folder that causes this problem exactly though, maybe something got corrupted or some sort of caching conflict, who knows...

MVC Web Api error 403.14

I am doing an MVC5 Web API Application. I am doing an simple example.
Create an Web Asp.Net web Application.
Select Empty and API.
Then I add a Api2 Controller called Home, and add a Simple Method called Get()
Method Get() looks like this.
public string Get()
{
return "Hello World";
}
I run the application and complete the URL.
http://localhost:56464/Home/Get
Got an error
Error HTTP 404.0 - Not Found
I test changing WebApiConfig adding
{action}
but I get the same error.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
When I start the Application http://localhost:56464/, I got this error
Error HTTP 403.14 - Forbidden
I always run the Application from Visual Studio 2013. I did not publish it it IIS
What is missing?
There are a few issues.
First the web api route template is
api/{controller}/{action}/{id}
Note the api prefix.
So that would mean that you have to browse to
http://localhost:56464/api/Home/Get
http://localhost:56464/ wont work because the route has the api prefix. So the forbidden error is default for what ever is hosting at that address.
To be able to use the URL you want in the question you would need to change the route template to match your desired template.

How to prevent application pool stop, on POST requests?

I have a web api service placed on https sub domain. I used castle Windsor to create all classes (per web request), from controller to domain objects.
I tested all actions on local machine and also using fiddler. everything works fine. And no memory leak was happened. and also we have a mobile programmer which worked with all actions and again every thing was fine.
but after hosting on https, by each request to web api urls, application pool stops.
but the request to authorization works fine. What is wrong with my application? here is my code to Config file:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, IAppBuilder app)
{
// Web API configuration and services
var container =Bootstrapper.WireUp();
ConfigCors(container, config);
RegisterControllers(container);
UserManagement.Config.Bootstrapper.WireUp(container);
config.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));
container.Register(Component.For<IDataProtectionProvider>().ImplementedBy<DpapiDataProtectionProvider>()
.UsingFactoryMethod(x => app.GetDataProtectionProvider()));
// Web API routes
config.MapHttpAttributeRoutes();
config.AddFiveLevelsOfMediaType();
config.Routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = #"\d+" });
}
private static void ConfigCors(IWindsorContainer container, HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
}
There is no specific reason that application pool is stopping. It could be a windows registry error, system error or assembly permission error. The best way to find out why is to go and check event viewer.
From Administrative Tools select Event Viewer
Alternatively, press Windows Key and type Event Viewer. Once there look into the into Windows Logs and then Application logs. Search for any error (red icon) or warning (yellow icon) that is related to IIS / ASP.Net or .Net framework. I have attached a sample image from my system -
Another Instance -
** EDIT **: There is one other possibility since it stopped after enabling https. Check the SSL certificate. The most common reason is using same certificate in multiple sites. Only wild card certificates can be used in multiple site. If the certificate is self signed, delete it and create a new one with * , being the prefix for the certificate name. Then use that one.

asp.net mvc api returning 'This XML file does not appear to have any style information associated with it. The document tree is shown below.'

I made a simple net mvc web api using entity framework, when I make an api call I am getting this error in chrome:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
in IE this error does not appear, instead the result of the api leads to a download of a json file... I want the response to be set in such as way that the results will appear in the browser, and not as a download, how do I do this?
Add the following code inside WebApiConfig.cs under config.MapHttpAttributeRoutes();
config.MapHttpAttributeRoutes();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And Global.asax.cs inside Application_Start() function add the following
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
A web api application does not have an inferface by default. When you call a web api, you should expect some data on the response body in a specified format to read on the client. Given that, the web api is returning the content formated as a xml and the browser shows it.
The message you have posted is a default message of the browser when you do not have a style for the xml content. It also happen on other platforms, given it is a message of the browser.
If you need to show a friendly interface, I suggest you change from asp.net web api application to an asp.net mvc application. You can have both on the same application, and in the case to show an user interface, just return a View.

Categories