【问题标题】:ASP.NET MVC and URL RewritingASP.NET MVC 和 URL 重写
【发布时间】:2011-08-24 11:16:18
【问题描述】:

在我的控制器中,视图有 4 个 ActionResult(显示、搜索、修改和删除)。对于最后 3 个,有一个 RedirectToAction() 作为 Actionesult 并且在 Route 我有一个这样的自定义:

routes.RouteMap("Detail", "/Show/{id}", new { controller : "Administration", action : "Show", id : UrlParameters.Optional });

当我得到搜索结果时,我需要在 url 中添加 2 个参数。这 2 个参数在 POST 中发送。 如何在 url 重写中按原样添加此参数?

当我来到观景台时

http://localhost/Show/1

搜索后

http://localhost/Show/1/foo/foo

感谢您的帮助:)

[编辑] 经过一番测试,我找到了解决方案。 除非显示 (GET | POST),否则表单和控制器都在 POST 中。

有2条路线:

routes.MapRoute(
                "RechercheEtablissementGucps",
                "DetailGucps/{idGucps}/{CategorieEtablissementValue}/{SearchField}",
                new { controller = "Administration", action = "AfficheDetailGuCPS", idGucps = UrlParameter.Optional, CategorieEtablissementValue = UrlParameter.Optional, SearchField = UrlParameter.Optional }
            );

routes.MapRoute(
                "Gucps", // Route name
                "DetailGucps/{idGucps}", // URL with parameters
                new { controller = "Administration", action = "AfficheDetailGuCPS", idGucps = UrlParameter.Optional } // Parameter defaults
            ); 

如果我进行搜索,我将获得所需的参数,如果完成另一个操作,则没有任何参数

/DetailGucps/29/DIR/fr

【问题讨论】:

  • 您不能在 URL 中放置 POST。
  • 嗯是的,但我不能在控制器中或通过路由添加这个?
  • 正如@Slaks 所说,您不能在 URL 中放置 POST。而且因为使用路由,您正在定义您的 url 的外观,好吧,您不能。
  • 如果我更新 GET 中的所有方法,我有 404 错误:(

标签: c# asp.net-mvc url-rewriting


【解决方案1】:
routes.RouteMap("Detail", "/Show/{id}/{p1}/{p2}", new { controller : "Administration", action : "Show", id : UrlParameters.Optional, p1: UrlParameters.Optional, p2: UrlParameters.Optional });

并将新参数添加到目标操作签名中。

【讨论】:

  • 我已经添加了一个如上所述的路由并且它可以工作:),但是当我有任何参数时,url 包含这个“System.Web.Mvc.UrlParameter,System.Web.Mvc.UrlParameter”。 url为null时如何删除参数?
  • action 的参数是否可以为空?
  • 我可以看看你的动作签名吗?
  • 这里是 Show Action 的签名:public ActionResult AfficheDetailGuCPS(Int64 idGucps, EquipeGUCps equipeGUCps, string categorieEtablissementValue, string searchField)String 可以为空,不是吗?
【解决方案2】:

从本质上讲,你所做的对我来说似乎是不正确的。

您似乎正在尝试将查询参数作为路由值传递。

还有一个问题是使用多个可选参数进行路由,请参阅:

http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

在您的操作中设置您期望的参数,例如:

public ActionResult Show(int ID, string param1 = null, int? param2 = null)
{
    return View(/*.GetShow(ID, param1, param2)*/);
}

[HttpMethod.Post]
public ActionResult Show(FormCollection collection)
{
    return RedirectToAction("Show", new { ID = collection["ID"], param1 = collection["param1"], param2 = collection["param2"] });
}

如果你明白了:)

【讨论】:

  • 例如谢谢你,我会尝试用这个更新我的代码。我也在寻找带逗号的参数。
【解决方案3】:

如果您希望将搜索结果发布到 Action,然后重定向到 Action,您将使用 [AcceptVerbs(HttpVerbs.Post)] 属性和 FormCollection,而不是在路由定义中命名 Post 参数。所以你应该只定义 Show 路线:

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

HttpVerb 属性将确保您的帖子在发布时被正确路由:

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Show(int? id)
    {
        var showViewModel = new ShowViewModel();

        // ... populate ViewModel

        return View(showViewModel);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Modify(FormCollection form)
    {
        var id = form["id"];
        var p1 = form["p1"];
        var p2 = form["p2"];

        // ... Modify

        return RedirectToAction("Show", new { id = id });
    }

【讨论】:

  • 不是我的愿望 ;) 我需要 RedirectToAction 并将搜索结果附加到 url 中。我尝试了这个例子,但没有添加参数。
  • @User.Anonymous - 在你的问题中你说你希望通过 POST 来实现这一点,但这是不可能的
  • 我尝试使用 GET 中的所有 4 种方法(在表单和控制器中),但现在我有 404 错误...
猜你喜欢
  • 2011-12-22
  • 2023-03-15
  • 2011-02-08
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 2013-08-28
相关资源
最近更新 更多