ASP.. NET:路径代替URL参数
本文关键字:URL 参数 路径 NET ASP | 更新日期: 2023-09-27 18:05:34
我一直在ASP中使用URL参数。. NET如下:
www.mysite.com/thread.php?id=123
但是,我想使用您经常看到的更干净的方法,它看起来像:
www.mysite.com/thread/123
我如何在ASP.NET中做到这一点(获得参数)?建立这样一个系统的一般程序是什么?
这叫做Url重写。如果您正在使用ASP。. NET-MVC框架,你得到这种默认行为,以及一个设计模式,帮助使开发更容易。
如果您试图将此内容硬塞到现有应用程序中,我建议您查看一些url重写模块。
如果我理解正确的话,这就是你要找的:
Asp。Net URL路由
路径样式参数可以像这样在c#控制器中访问。要检索的路径参数为"id",返回值为"123"。
MVC路由public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
}
//Test URL: http://localhost/MyProject/Test/GetMyId/123
MyController.cs
public class TestController : Controller
{
public string GetMyId()
{
//ANSWER -> ControllerContext.RouteData.Values["id"]
return ControllerContext.RouteData.Values["id"].ToString();
}
}