1

I have written below code to achieve URL Rewriting.

In Global.asax

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

In View

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

When i click link, it goes to action of controller which is perfect.

But my URL is

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

and i don't want that.

I just want my URL as

 https://localhost/StudentDetail

What change should I do to achieve above URL?

4

2 回答 2

0

如果您希望 SortField 的值始终为 Major,但不显示在 Url 中,则只需执行以下操作:

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

但是,如果 Major 出现在您的 Url 中,这意味着当您生成链接时,您会以某种方式将其传递给帮助程序。您需要确保从帮助程序中删除它。如果您仍想接受一个字段,但默认为 Major,那么您可以这样做:

routes.MapRoute(
   "StudentDetail", // Route name
   "StudentDetail/{SortField}", // URL with parameters
   new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
         SortField = "Major" }
);
于 2013-08-26T15:39:47.047 回答
0

有很多方法涉及 TempData 或 Session 以某种方式存储参数并GET使用干净的链接调用:

<a href="/StudentDetail" >Students</a>

但是您可以使用更一致的不同方法,将GET操作更改为POST并将SortField内部发送到隐藏字段,就像这样开始,因为我没有测试它:

@using (Html.BeginForm("UAboutMeStudentDetails", "UDashboard", new { SortField = "Major" }, FormMethod.Post, null))
{
    @Html.Hidden("SortField", "Major")
    <button type="submit">Students</button>
}
于 2013-08-26T15:24:51.250 回答