Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC5 refuses to map a route which matches a physical path

When I debug/run, using IIS Express, and browse to http://localhost:1234/People, IIS Express tries to browse the People directory instead of executing the People route, and I get a 403.14 HTTP error. So I disabled the StaticFile handler in the Web.config and refreshed. Now I get a 404.4 HTTP error:

404

I know that the route works because if I rename the RoutePrefix, e.g. PeopleTest, then the route is executed and I get the response I expect.

How can I convince IIS/Express to prefer MVC routes over static files/directories?

I am using attribute routing; the relevant code is below:

Web.config

<system.webServer>
    <modules>
        <remove name="FormsAuthentication" />
    </modules>

    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="TRACEVerbHandler" />
        <remove name="StaticFile"/>
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

Global.asax

GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacConfig.Configure();

Startup\WebApiConfig

namespace MyApi.Startup {
    public static class WebApiConfig {
        public static void Register(HttpConfiguration config) {
            config.MapHttpAttributeRoutes();
        }
    }
}

People\PeopleController

namespace MyApi.People {
    [RoutePrefix("People")]
    public partial class PagesController : BaseController {
        [Route]
        [HttpGet]
        [ResponseType(typeof(IEnumerable<Person>))]
        public IHttpActionResult Get() { ... }
    }
}

Note that since I'm using attribute routing, I am using a non-standard folder structure. E.g. I don't have the Controllers/Models/Views folders, instead I have root folders for each business area (e.g. ~\People contains the controllers/models/etc. for the "People" business area).

What I've Tried

  1. Setting RAMMFAR.
  2. Removing and re-adding ExtensionlessUrlHandler-Integrated-4.0.
like image 219
Josh M. Avatar asked Dec 16 '15 14:12

Josh M.


1 Answers

Fixed by adding setting RouteExistingFiles = true:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }
}

So that ASP.NET routing will handle all routes: https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles(v=vs.110).aspx

like image 154
Josh M. Avatar answered Oct 31 '22 11:10

Josh M.