c# - How to use RouteConfig to append a page URL -
i struggling code work, think i've read enough suggest correct way approach this.
on intranet, i'd user type in single word search textbox, , check checkbox
. when new page loads, i'd url rewritting services of asp.net mvc kick in , change value from
mysite.com/?id=blah&ischecked=true
to
mysite.com/home/index/blah/true
my code isn't working in sense of gives no error, doesn't explaining. so, i've removed check box focus on textbox.
my route is
routes.maproute( name: "default", url: "{controller}/{action}/{mytype}", defaults: new { controller = "home", action = "index", mytype = urlparameter.optional } );
my controller
public actionresult index() { viewbag.message = "modify this"; return view(); } [httpget] public actionresult index(string mytype) { viewbag.message = "..." + mytype; return view(); }
and view has
@using (html.beginform("index", "home",formmethod.get)) { <input name="mytype" /><br /> <input type="submit" /> } @html.actionlink("click me", "index", new { @mytype = "blah" }) //renders correctly
the problem is, shows querystring still in address bar
mysite.com/?mytype=mysearchvalue
instead of
mysite.com/home/index/mysearchvalue
you can't purely routing because browser send form values query string parameters when part of request. once request has been sent server, mvc framework can't url used.
this leaves 1 real option (assuming don't want send custom request using javascript), explicitly redirect desired url (meaning have 2 requests when form submitted).
the simplest way of doing in controller (rather, in separate controller ensure there no conflict in method signatures):
public class formcontroller : controller { public actionresult index(string mytype) { return redirecttoaction("index", "mypropercontroller", new { mytype }); } }
if direct form controller action, mvc use routing engine generate proper url real action , redirect browser accordingly.
you same controller action involve inspecting request url check whether query string used or not , redirecting same action, little odd.
Comments
Post a Comment