2013-03-08 52 views
0

我很新的ASP.net和最近从asp.net转换到asp.net MVC项目。 我有一个asp.net MVC Url.Action的基本问题。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 Like/Question/Answer?QuestionID = 10 & Response = 1,这个URL匹配路由定义并正确调用Action。一切安好。

但我的实际要求是这些参数名称不应该暴露。它应该像 /Question/Answer/10/1

我不想在String.Format的帮助下手动设置此链接的格式,因为我的网站可能放置在虚拟目录中而不是默认网站。

+0

你可以试试这个http://www.codeproject.com/Articles/576514/AplusBeginner-splusTutorialplusonplusVariousplus – Abdul 2015-05-22 10:52:23

回答

0

因为ActionLink将执行GET请求,所以这些值将始终在查询字符串中。从询问字符串中删除questionId和响应的一种方法是使用表单对带有questionId和响应参数的答复操作执行POST。

其他选项是执行ajax请求并更新响应中的dom。