ASP.NET MVC Routing not Working in Virtual Directory

淺唱寂寞╮ 提交于 2019-12-12 16:35:55

问题


I have an asp.net mvc 2 app (using .net 4.0) that isn't routing correctly when hosted in a virtual directory. I have the following simple routing rule:

        routes.MapRoute(
            "Default", // Route name
            "{action}", // URL with parameters
            new { controller = "accounts" } // Parameter defaults
        );

I'm trying to resolve http://mydomain.com/accounts/new. Where "accounts" is a virtual directory. If I put the app in the root of an IIS website it routes fine for http://mydomain.com/new, but if I put the app in a virtual directory I get 404 errors. I've debugged and it is executing global.asax and configuring routing when in the vdir. Is there something special I need to do for routing in a virtual directory?

FYI. I'm using a vdir because the root has wordpress in it.

Thanks!

one more thing is that if I specify a default action in parameter defaults, it will execute the default action/controller, but it never matches anything else.


回答1:


I figured it out. Wordpress (which I had installed at the website root) had configured some URL rewrite rules which were preventing asp.net mvc receiving any requests other than the virtual directory root. Anything with a path beyond that was being rewritten to index.php which of course didn't exist in my mvc app.

I removed the rewrite rule, and now everything works as expected.




回答2:


does it work if you change it to:

routes.MapRoute( 
    "Default", // Route name 
    "accounts/{action}", // URL with parameters (BUT WITH ACCOUNTS PREFIX)
    new { controller = "accounts" } // Parameter defaults 
); 


来源:https://stackoverflow.com/questions/3297178/asp-net-mvc-routing-not-working-in-virtual-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!