问题描述
谁能帮我?我做的ASP.NET MVC一些测试。
Can anyone help me? I'm doing some tests in ASP.NET MVC.
我要实现的漂亮的URL作为stackoverflow.com路由系统。
I want to implement the nice URL as stackoverflow.com routing system.
例如:
stackoverflow.com/questions/1074/asp-mvc
domain.com/id/title
For example: stackoverflow.com/questions/1074/asp-mvcdomain.com/id/title
下面是我的code:
-
在Global.asax
in the Global.asax
routes.MapRoute(NULL,
的帖子/ {帖子ID} / {标题},
新{控制器=帖子,行动=详细信息,后=(字符串)空},
新{=帖子ID @\\ D +,标题= @([A-ZA-Z0-9 - ] +)}
);
routes.MapRoute(null, "posts/{PostId}/{Title}", new { controller = "Posts", action = "Details", post = (string)null }, new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" } );
在View:
<%= Html.ActionLink(Model.Title,详细信息,新的{Model.PostID,Model.Title})%>
<%= Html.ActionLink(Model.Title, "Details", new { Model.PostID, Model.Title})%>
使用这些codeS,我歌厅的网址:
With those codes, I am geting the url : http://localhost:53171/posts/Details?PostID=5&Title=Test-title
一一能否告诉我?
非常感谢你。
推荐答案
不知道所有的计算器网址的手段,但如果你想要一个干净的网址,如:
Not sure what all of the stackoverflow url means, but if you wanted a clean url like:
http://stackoverflow.com/Questions/132/thetitle
在您的Global.asax:
In your Global.asax:
routes.MapRoute("PostController",
"Questions/{post_id}/{post_title}",
new { controller = "Post", action = "Index" },
new { post_id = @"\d+", post_title = @"([A-Za-z0-9-]+)" }
);
在你的控制器中检索值:
In your controller retrieve the values:
public ActionResult Index(Int32 post_Id, String post_title)
{
return View();
}
要产生正确的网址使用Html.RouteLink:
To produce the correct url use Html.RouteLink:
<%= Html.RouteLink("The Title", "PostController", new { post_id = 132, post_title = "thetitle" })%>
这篇关于如何得到一个不错的URL像&QUOT; HTTP://stackoverflow.com/questions/1074/asp-mvc"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!