【问题标题】:URL Routing Not Working in ASP .Net MVC2URL 路由在 ASP .Net MVC2 中不起作用
【发布时间】:2013-09-01 20:50:11
【问题描述】:

我正在使用 ASP.Net MVC2 应用程序。 因为我使用了 URL 路由

获取网址

    https://localhost/StudentDetail/SortField

我在 Global.asax 中编写了以下代码

routes.MapRoute(
"StudentDetail", // Route name
"StudentDetail/{SortField}", // URL with parameters
new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
     SortField = "Major" }
);

在我看来,链接如下

<a href="/StudentDetail?SortField='Major'" >Students</a>

但它不起作用。我的网址是

https://localhost/StudentDetail?SortField='Major'

谁能帮我获取所需的网址..?

我希望 URL 为

https://localhost/StudentDetail/SortField

提前致谢,Prashant

【问题讨论】:

  • &lt;a href="/StudentDetail/Major" &gt;Students&lt;/a&gt; 应该可以工作。
  • @AbbasAmiri 它对我有用。

标签: c# asp.net-mvc url-rewriting asp.net-mvc-routing url-routing


【解决方案1】:

我认为您对路由的工作原理有错误的想法。您的路线:

routes.MapRoute(
"StudentDetail", // Route name
"StudentDetail/{SortField}", // URL with parameters
new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
     SortField = "Major" }
);

将采用 SortFeild 参数(Major、Gpa 等),并将 {SortField} 替换为该值。因此,使用以下操作链接:

@Html.ActionLink("Student Details", "UAboutMeStudentDetails", new {controller="UDashboard", SortField = "Major}) 

将生成以下 HTML

<a href="/StudentDetail/Major">Student Details</a>

请注意,SortField 的值已替换您路线中的 {SortField} 参数。您永远不会得到一个看起来像您所请求的 URL,因为您如何将 SortField 的值用于操作?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2013-04-06
    相关资源
    最近更新 更多