1

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 :

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
4
  • 2
    You cannot put a POST in a URL. Commented May 12, 2011 at 20:57
  • Hmm yes, but I can't add this in the Controller or by the Route ? Commented May 12, 2011 at 21:03
  • as @Slaks said you cannot put a POST in a URL. And because with routes you are defining how your urls will look like, well, you can't. Commented May 12, 2011 at 21:13
  • If i update all methods in GET, I have 404 Error :( Commented May 13, 2011 at 9:59

3 Answers 3

1
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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have add a Route as above and it works :), but when I have any parameters, the url contains this "System.Web.Mvc.UrlParameter,System.Web.Mvc.UrlParameter". How to remove the parameters from url when it's null ?
are the params on the action nullable?
can i see your action signature?
Here the signature of Show Action : public ActionResult AfficheDetailGuCPS(Int64 idGucps, EquipeGUCps equipeGUCps, string categorieEtablissementValue, string searchField) String can be null, no ?
1

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 :)

1 Comment

Thank you for example, I'll try to update my code with this. I'm looking for parameters with commas too.
0

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

3 Comments

Not really my wish ;) I need to RedirectToAction and append the result of the search in the url. I try this example, but no where a parameter is added.
@User.Anonymous - in your question you say you wish to achieve this with a POST but this is not possible
I try with all my 4 methods in GET (in form and in Controller), but now I have 404 Error...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.