开发者

ASP.NET MVC and URL Rewriting

In my Controller, I have 4 ActionResult (Show, Search, Modify and Delete) for the View. For the 3 last, there are a RedirectToAction() as Actionesult and in Route I have a custom as this :

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

I need to add 2 parameters in url when I get the result of Search. This 2 parameters are sent in POST. How to add this parameters in url rewritting as is ?

When I come on the View

http://localhost/Show/1

After a Search

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

Thanks for helping :)

[EDIT] After some test, I found the solution. Forms and Controller are in POST unless the Show (GET | POST).

There is 2 routes :

开发者_Python百科
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
            ); 

I have then the parameters as desired if I search and nothing if another Action is done

/DetailGucps/29/DIR/fr


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

and add the new params to the target actions signature.


in essence what you are doing seems incorrect to me.

It seems like you are trying to pass query parameters as route values.

Also there is an issue with using more than one optional parameter for routing, see:

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

In your action set your parameters you are expecting, ex:

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

If you get the idea :)


If you wish to post the results of search to an Action and then redirect to an Action you would use the [AcceptVerbs(HttpVerbs.Post)] attribute and FormCollection instead of naming Post parameters in your routing definition. So you should only define the Show route:

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

The HttpVerb attributes will ensure that your post is routed correctly when posting:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜