問題描述
我正在嘗試在我的 asp.net mvc 項目中路由一個 .aspx(webforms 頁面).我在 global.asax 中注冊頁面:
I'm trying to route a .aspx (webforms page) in my asp.net mvc project. I register the page in global.asax:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Reports/Tickets.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
問題是,在我添加第二行之后,站點停止進入我的主控制器(索引操作)并重定向到:http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22
總是我運行項目.
The problem is, after i add the second line, the site stops to enter in my Home Controller (Index Action) and is redirecting to: http://localhost:37538/Reports/Tickets?action=Index&controller=Login%22
always that i run the project.
項目詳情:
- Asp.Net MVC 3
- 表單驗證
- .Net 4.0
Obs:要重現此錯誤,請在 /WebForms/Reports
文件夾中創建 Tickets
webforms 頁面后,創建一個新的 asp.net mvc 項目作為 Internet 應用程序,并注冊新路線.運行項目(可能您已登錄),現在注銷,您將被重定向到 http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account
,為什么?
Obs: to reproduce this error, create a new asp.net mvc project as internet app, after create the Tickets
webforms page inside a /WebForms/Reports
folder, and register the new route. Run the project (probably you're logged), so now logoff and you will be redirected to http://localhost:35874/Reports/Tickets?action=LogOff&controller=Account
, so why?
推薦答案
解決了! 所以,我們需要在 webforms 路由中添加一個路由約束,以確保它只捕獲傳入的路由,而不是傳出路由生成.
Solved! So, we need to add a route contraint to the webforms route to ensure that it only catches on incoming routes, not outgoing route generation.
將以下類添加到您的項目中(在新文件中或 global.asax.cs 的底部):
Add the following class to your project (either in a new file or the bottom of global.asax.cs):
public class MyCustomConstraint : IRouteConstraint{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){
return routeDirection == RouteDirection.IncomingRequest;
}
}
然后將Tickets路由更改為以下:
Then change the Tickets route to the following:
routes.MapPageRoute(
"Tickets",
"Reports/Tickets",
"~/WebForms/Reports/Tickets.aspx",
true, null,
new RouteValueDictionary { { "outgoing", new MyCustomConstraint() } }
);
這篇關于將 MapPageRoute 添加到 asp.net mvc 項目后,站點停止進入 Home Controller的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!