【问题标题】:Disabling querystrings and using only paths with /禁用查询字符串并仅使用带有 / 的路径
【发布时间】:2011-09-25 17:05:20
【问题描述】:

我正在使用表单和随后的操作来重定向到另一个页面。问题是动作重定向函数总是使用查询字符串而不是完整路径重定向。 结束网址是: localhost/Get?Description="ddd"&Id=555 而我希望它是: 本地主机/获取/ddd/222

这是我的代码: 我的路线:

  routes.MapRoute(
            "Create", // Route name
            "{controller}/{action}", // URL with parameters
            new
            {
              controller = "Home"
            } // Parameter defaults
           );

  routes.MapRoute(
                  "Get", // Route name
                  "{controller}/{action}/{Description}/{id}/", // URL with parameters
                  new
                  {
                    controller = "Home",
                    action = "Get",
                    Description = UrlParameter.Optional,
                    id = UrlParameter.Optional
                  } // Parameter defaults
                 );`

表单填写描述和 id 并触发 create 操作,我将在其中重定向到 get 操作:

    public ActionResult Create(string desc,string id)
    {
      System.Web.Routing.RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary(new { Description = desc, Id = id });

      return RedirectToAction("Get",route);
    }

最终触发 get 操作。

    public ActionResult Get(String Description,String Id)
    { return View() }

请帮忙..

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-routing


    【解决方案1】:

    可能您需要使用 RouteTable.Routes.GetVirtualPath 生成虚拟路径。请参考this article。

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 2011-07-10
      • 1970-01-01
      • 2012-04-01
      • 2011-01-16
      • 2016-03-26
      • 2016-06-28
      • 2019-01-02
      • 2014-05-01
      相关资源
      最近更新 更多