2

By testing and wasting obscene amount of time I have found out that ASP.NET MVC has a bug which prevents using the string "api" in request URL. I wan to access my method with URL like this

www.mysite.com/api/test

This is not an unexpected wish. In fact it an an obvious Url choice.

Is there a workaround to achieve this?

UPDATE

By request routing definition.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // this executes, checked it in debugger
    routes.MapRoute(
        name: "Test",
        url: "api/test",
        defaults: new { controller = "Api", action = "Test" }
    );
}

ApiController

public class ApiController : Controller
{
    public ActionResult Test()
    {
        return Content("TEST TEST TEST");
    }

{
8
  • Can this really be true? Commented May 30, 2015 at 16:39
  • Show your route definition(s). Commented May 30, 2015 at 16:44
  • @Robert Believe me. It is true, I have wasted two days on this, It is hard to believe but it is. Commented May 30, 2015 at 16:53
  • 1
    I test it at my computer. it works fine. Would you Show your (Api) controller? Commented May 30, 2015 at 18:40
  • 1
    Do you also have the WebAPI packages installed? If so, that's part of the default routing in the WebApi's config class. Commented May 30, 2015 at 18:53

1 Answer 1

2

If you have the WebApi packages installed, you'll find a WebApiConfig.cs class in App_Start. Here's what it looks like:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

So, assuming you don't change the default code in Global.asax.cs, this route gets added to the routing table. Hence, why your /api/whatever route doesn't work.

If you're not using WebApi, I would suggest removing the packages. Otherwise, you can simply change the "root" part of the API route to something else:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            /* changed 'api' to 'restsvc' */
            routeTemplate: "restsvc/{controller}/{id}", 
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes. There is a WebApiConfig as you described. If I am correct this is a static class with static method soi it executes before everything and takes precedence above my route. But why is this so? I have never added this, thsi si completely undocumented. Nowhere have I found a single information about that. At this point I am pretty upset about this microsoft stupidity, I would be grateful if you have a link to somer resource which would shed more light on the subject. ASP.NET MVC as it is is a trap.
He doesn't need to update WebApiConfiq. It will works fine if he puts GlobalConfiguration.Configure(WebApiConfig.Register) after RouteConfig.RegisterRoutes(RouteTable.Routes)
@Mahedi Thanks. Both of your proposals sjhow just how badly is MVC designed, There is no hint of encapsulation.
Not part of my question though, But it still remains open why is this thing setup like that. How to prevent MS stupidities like this in the future.
@f470071 It's part of the T4 templates for MVC4. The tooling for MVC5 makes it easier to leave the WebAPI bits out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.