Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC和URL重写_C#_Asp.net Mvc_Url Rewriting - Fatal编程技术网

C# ASP.NET MVC和URL重写

C# ASP.NET MVC和URL重写,c#,asp.net-mvc,url-rewriting,C#,Asp.net Mvc,Url Rewriting,在我的控制器中,我有4个ActionResult(显示、搜索、修改和删除)用于视图。对于最后3个,有一个RedirectToAction()作为ActionResult,在Route中我有一个如下自定义: routes.RouteMap("Detail", "/Show/{id}", new { controller : "Administration", action : "Show", id : UrlParameters.Optional }); 当我得到搜索结果时,我需要在url中添加

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

routes.RouteMap("Detail", "/Show/{id}", new { controller : "Administration", action : "Show", id : UrlParameters.Optional });
当我得到搜索结果时,我需要在url中添加2个参数。这两个参数以邮寄方式发送。 如何按原样在url重写中添加此参数

当我来到风景区

http://localhost/Show/1
搜索后

http://localhost/Show/1/foo/foo
谢谢你的帮助:)

[编辑]经过一些测试,我找到了解决方案。 表格和控制人员都在岗位上,除非演出结束(GET | POST)

有两条路线:

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

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

如果您希望将搜索结果发布到操作,然后重定向到操作,则可以使用[AcceptVerbs(HttpVerbs.post)]属性和FormCollection,而不是在路由定义中命名post参数。因此,您应该只定义放映路线:

        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 });
    }

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

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

此外,使用多个可选参数进行布线也存在问题,请参阅:

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

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"] });
}

如果你有这样的想法:)

你不能在URL中放置帖子。嗯,是的,但我不能将其添加到控制器或路径中?正如@Slaks所说,你不能在URL中放置帖子。而且,由于使用路由,您正在定义url的外观,因此,您不能。如果我更新GET中的所有方法,我会出现404错误:(我已如上所述添加了一个路由,它可以工作:),但当我有任何参数时,url包含以下内容“System.Web.Mvc.urlparmeter,System.Web.Mvc.urlparmeter”。当url为空时,如何从url中删除参数?操作上的参数是否可以为空?我可以看到您的操作签名吗?这里是Show action的签名:
public ActionResult AfficheDetailGuCPS(Int64 idGucps、EquipeGUCps EquipeGUCps、string CategileTablesElementValue、string searchField)
string可以为空,不?这不是我的愿望;)我需要重定向到操作并将搜索结果附加到url中。我尝试了这个例子,但没有添加参数。@User.Anonymous-在你的问题中,你说你希望通过一篇文章来实现这一点,但这是不可能的。我尝试了GET中的所有4个方法(在form和Controller中),但现在我有404错误…例如,谢谢你,我将尝试用这个更新我的代码。我也在寻找带有逗号的参数。
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"] });
}