Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi routing for asp.net WebForms returns 404

I'm having some troubles configuring WebApi routes in an asp.net WebForms app.

In the global.asax file, I have something like this:

void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

I have another file that has this code:

using System.Web.Http;

namespace WebConfig
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}",
                        defaults: new { id = System.Web.Http.RouteParameter.Optional });
        }
    }
}

Then, in the root, I have a folder called api and in that folder I have a file that contains a class that looks somewhat like that:

public class MyController : ApiController
{
    [Route("MyController")]
    [HttpPost]
    public HttpResponseMessage Post([FromBody]string value)
    {....}

And then finally, in my client code, I have this ajax call:

$.ajax({
    url: "/api/MyController",
    contentType: "application/json; charset=utf-8",
    type: "POST",
    dataType: "json",
    cache: "false",
    data: someData});

However, when I run this code, all I get is a 404 error. What do I need to change in the routing mechanism to make this work?

like image 714
frenchie Avatar asked May 07 '14 22:05

frenchie


2 Answers

I think others were very close. Try this:

[RoutePrefix("api")]  // or maybe "api/", can't recall OTTOMH...
public class MyController : ApiController
{
    [Route("MyController")]
    [HttpPost]
    public HttpResponseMessage Post([FromBody]string value)

and then request /api/MyController

If this doesn't work, use RouteDebugger to analyze your routes, and why it rejects a match. Amend your question with what you see in RouteDebugger so I can trace down what is not matching.

Also, you may need to call MapHttpAttributeRoutes in your Register function - but not sure about that.


Edit

Now that I'm looking at it again, I think I see more issues with it.

First, let's start with the template:

Here is what you have (from your question):

config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}",
                    defaults: new { id = System.Web.Http.RouteParameter.Optional });

The odd part here is that your template does not have {id} segment, but is defined as optional. Sounds like it is missing from the template, and the route should be changed to:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = System.Web.Http.RouteParameter.Optional });

Note that you also removed default action - I'm not sure if MVC automatically uses convention method of locating a method called Post, but I suppose it does.

Second problem is that your method signature is (again from your question):

public HttpResponseMessage Post([FromBody]string value)

It defines Post that takes in a parameter named value, whereas your route defines parameter named id. Hence there is another mismatch. You can rename the variable or decorate (see below). Also, {id} is marked optional, but I believe (and here I don't remember exactly OTTOMH) you need to give default value to to value for those cases where {id} is not supplied, and so combining together:

public HttpResponseMessage Post([FromBody(Name="id")]string value = null)

This should fix it, although there may be more issues... but let's start with these.

like image 142
LB2 Avatar answered Oct 19 '22 16:10

LB2


Change url: "/api/MyController" to url: "/api/My"

like image 36
Kiran Avatar answered Oct 19 '22 16:10

Kiran