C# 如何在 asp.net mvc 3 项目中路由 .aspx 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10175200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to route a .aspx page in asp.net mvc 3 project?
提问by Vinicius Ottoni
I have a .aspx page in the following path:
我在以下路径中有一个 .aspx 页面:
Areas/Management/Views/Ticket/Report.aspx
I want to route that to the following path in my browser:
我想将其路由到浏览器中的以下路径:
http://localhost/Reports/Tickets
How can i do that?
我怎样才能做到这一点?
I try this:
我试试这个:
routes.MapRoute(
"Tickets", // Route name
"Areas/Management/Views/Ticket/Report.aspx", // Original URL
new { controller = "Reports", action = "Tickets" } // New URL
);
But i got the 404error.
但我得到了404错误。
What i'm doing wrong?
我在做什么错?
Obs: I put that before the Defaultroute.
Obs:我把它放在Default路线之前。
采纳答案by Vinicius Ottoni
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.
解决了!因此,我们需要向 webforms 路由添加一个路由约束,以确保它只捕获传入路由,而不是传出路由生成。
Add the following class to your project (either in a new file or the bottom of global.asax.cs):
将以下类添加到您的项目中(在新文件中或 global.asax.cs 的底部):
public class MyCustomConstaint : IRouteConstraint{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){
return routeDirection == RouteDirection.IncomingRequest;
}
}
Then change the Tickets route to the following:
然后将 Tickets 路由更改为以下内容:
routes.MapPageRoute(
"Tickets",
"Reports/Tickets",
"~/WebForms/Reports/Tickets.aspx",
true, null,
new RouteValueDictionary { { "outgoing", new MyCustomConstaint() } }
);
回答by Parv Sharma
you are doing it opposite. this maps your url Areas/Management/Views/Ticket/Report.aspxto { controller = "Reports", action = "Tickets" }
what u should do instead is
set the url as Reports/TicketsEDIT:- you can create a routeHandler just for routing to this .aspx page.. like this.
你正在做相反的事情。这将您的 url 映射Areas/Management/Views/Ticket/Report.aspx到 {controller = "Reports", action = "Tickets" }
你应该做的是将 url 设置为Reports/TicketsEDIT:- 您可以创建一个 routeHandler 只是为了路由到这个 .aspx 页面..像这样。
public class ASPXRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return BuildManager.CreateInstanceFromVirtualPath("~/Areas/Management/Views/Ticket/Report.aspx", typeof(Page)) as Page;
}
}
then u can add ur route to the existing routes table using
然后你可以将你的路线添加到现有的路线表中
Route customRoute = new Route("Reports/Ticket",null, new ASPXRouteHandler());
routes.Add(customRoute);
回答by coffeeyesplease
if you leave the default routing when you create the asp.net project
如果在创建 asp.net 项目时保留默认路由
public class ReportsController : Controller
{
public ActionResult Ticket()
{
return View();
}
}
this should do the trick. The routing in asp.net mvc means that you don't link directly to .aspx but to Actions (methods) that in turn return an appropriate view (.aspx)
这应该可以解决问题。asp.net mvc 中的路由意味着您不直接链接到.aspx 而是链接到操作(方法),然后返回适当的视图(.aspx)
回答by Chris Diver
If you are trying to utilise web forms in a MVC project then I would move your .aspx out of the views folder, as it isn't really a view, so something like WebForms/Tickets/Report.aspx.
如果您尝试在 MVC 项目中使用 Web 表单,那么我会将您的 .aspx 移出视图文件夹,因为它不是真正的视图,因此类似于 WebForms/Tickets/Report.aspx。
In web forms you map a route by calling the MapPageRoutemethod.
在 Web 表单中,您可以通过调用该MapPageRoute方法来映射路线。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Tickets/Report.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
You'll need to put that before the default MVC route.
您需要将其放在默认 MVC 路由之前。

