Dis*_*ile 10 c# asp.net-mvc asp.net-mvc-routing asp.net-mvc-2
我创建了一个页面路由,因此我可以将我的MVC应用程序与我项目中存在的一些WebForms页面集成:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// register the report routes
routes.MapPageRoute("ReportTest",
"reports/test",
"~/WebForms/Test.aspx"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
每当我在视图中使用Html.ActionLink时,这就产生了一个问题:
<%: Html.ActionLink("Home", "Index", "Home") %>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中加载页面时,链接显示如下:
http://localhost:12345/reports/test?action=Index&controller=Home
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个?我怎样才能解决这个问题?
小智 6
我刚才有一个非常相似的问题.我的解决方案是在寻找ActionLink的匹配时为路由系统提供拒绝页面路由的理由.
具体来说,您可以在生成的URL中看到ActionLink创建了两个参数:controller和action.我们可以使用那些方法来使我们的"标准"路由(〜/ controller/action/id)与页面路由不匹配.
通过使用参数替换页面路径中的静态"报告",我们将其称为"控制器",然后添加"控制器"必须为"报告"的约束,我们为报告获取相同的页面路径,但拒绝任何不是"报告"的控制器参数.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// register the report routes
// the second RouteValueDictionary sets the constraint that {controller} = "reports"
routes.MapPageRoute("ReportTest",
"{controller}/test",
"~/WebForms/test.aspx",
false,
new RouteValueDictionary(),
new RouteValueDictionary { { "controller", "reports"} });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是你需要在MapPageRoute声明中添加一些参数选项.因此,如果WebForms目录中有多个webforms页面,则效果很好.
routes.MapPageRoute ("ReportTest",
"reports/{pagename}",
"~/WebForms/{pagename}.aspx");
Run Code Online (Sandbox Code Playgroud)
PS:你可能还想看看它的RouteExistingFiles属性RouteCollection
另一种方法是使用
<%=Html.RouteLink("Home","Default", new {controller = "Home", action = "Index"})%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6447 次 |
| 最近记录: |