Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NETMVC2中的URL重写_.net_Asp.net Mvc_Url Rewriting - Fatal编程技术网

ASP.NETMVC2中的URL重写

ASP.NETMVC2中的URL重写,.net,asp.net-mvc,url-rewriting,.net,Asp.net Mvc,Url Rewriting,我写了下面的代码来实现URL重写 在Global.asax中 鉴于 <a href="/StudentDetail?SortField='Major'" >Students</a> 我不想那样 我只想我的URL作为 https://localhost/StudentDetail 任何人都可以告诉我,我应该做什么改变,以实现上述网址 谢谢, Prashant有许多涉及TempData或Session的方法以某种方式存储参数并使用干净的链接调用GET: <a h

我写了下面的代码来实现URL重写

在Global.asax中

鉴于

 <a href="/StudentDetail?SortField='Major'" >Students</a>
我不想那样

我只想我的URL作为

 https://localhost/StudentDetail
任何人都可以告诉我,我应该做什么改变,以实现上述网址

谢谢,
Prashant

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

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

希望这能有所帮助。

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

routes.MapRoute(
   "StudentDetail", // Route name
   "StudentDetail", // URL with parameters
   new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
         SortField = "Major" }
);
但是,如果Major出现在您的Url中,这意味着当您生成链接时,您会以某种方式将其传递给帮助者。您需要确保将其从帮助器中删除。如果仍要接受字段,但默认为“主要”,则应执行以下操作:

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

我试过上面的代码,效果很好。现在我有一个问题,上面的代码是我为单个页面编写的,但在我的应用程序中,我有80个不同的页面,我必须编写80个不同的路由。所以问题是,这是在Global.asax中编写80条路由的好方法,还是我可以通过其他一些功能来实现。。。?如果我们在Global.asax文件中写入80条路由,您不认为性能会下降吗?@PrashantBhojani-如果您有80条不同的唯一路由,您没有太多选择。这就是为什么MVC默认使用约定,所以您不需要编写此类路由,而是选择使用特定路由。。话虽如此,一个简单的80条路由匹配将有一些性能开销,但您或您的用户可能不会注意到。我已经编写了上述代码,但我的url仍然是,因此我如何将其更改为…?@PrashantBhojani-显示您的完整路由集。很有可能还有另一条更为具体的路线在前方接手。
@using (Html.BeginForm("UAboutMeStudentDetails", "UDashboard", new { SortField = "Major" }, FormMethod.Post, null))
{
    @Html.Hidden("SortField", "Major")
    <button type="submit">Students</button>
}
routes.MapRoute(
   "StudentDetail", // Route name
   "StudentDetail", // URL with parameters
   new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
         SortField = "Major" }
);
routes.MapRoute(
   "StudentDetail", // Route name
   "StudentDetail/{SortField}", // URL with parameters
   new { controller = "UDashboard", action = "UAboutMeStudentDetails", 
         SortField = "Major" }
);