【问题标题】:ASP.net MVC Url.Action Link formattingASP.net MVC Url.Action 链接格式
【发布时间】: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 的帮助下手动格式化此链接,因为我的网站可能放置在虚拟目录中,而不是默认网站。

【问题讨论】:

标签: asp.net-mvc-4 url.action


【解决方案1】:

由于 ActionLink 将执行 GET 请求,因此值将始终位于查询字符串中。从查询字符串中删除 questionId 和 response 的一种方法是使用表单对 questionId 和 response 参数执行 POST 到您的回答操作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2015-03-22
    • 2020-12-29
    • 2010-12-18
    • 2011-09-18
    相关资源
    最近更新 更多