【发布时间】:2013-02-23 21:52:01
【问题描述】:
我对 ASP.net 非常陌生,最近从 asp.net 转换为 asp.net MVC 项目。 我在 asp.net MVC Url.Action 中有一个基本问题。
我的控制器中有一个ActionResult方法,两个参数QuestionId, Response,路由配置如下,
public class QuestionController : Controller
{
public ActionResult Answer(int QuestionId, string Response)
{
..do something.....
}
}
和路由配置映射为
routes.MapRoute(
name: "QuestionAnswer",
url: "{controller}/{action}/{QuestionId}/{Response}",
defaults: new { controller ="Question", action = "Answer", QuestionId = 0, Response = string.Empty}
);
从下面的语法调用html页面中的动作链接
@Html.ActionLink("Answer is 1", "Answer", "Question", null, null, null, new RouteValueDictionary(new {QuestionId = 10, Response = "1" }), null)
它形成 URL 像 /Question/Answer?QuestionID=10&Response=1,这个 url 匹配路由定义并正确调用 Action。一切顺利。
但我的实际要求是这些参数名称不应该暴露。它应该像 /问题/答案/10/1
我不想在String.Format 的帮助下手动格式化此链接,因为我的网站可能放置在虚拟目录中,而不是默认网站。
【问题讨论】: