c# - Using MVC3 is there a better way of removing previous routing values and also stopping them being used as a query string? -


When using a current URL, I have the following to find a way to prevent MVC3 from reusing existing root values :

"index", id = "no ", Id2 =" no "}); Routes.MapRoute (" justID "," {controller} / {action} / {id} / {id2} ", new {controller =" home ", action =" index "} ); Routes.MapRoute ("idAndId2", "{controller} / {action} / {id}", new {controller = "home", action = "index"});

And My index index.cshtml

  @ html.ActionLink (index with "id = 10", "index", new {id = 10}, zero); @html.exelink ("id 2 = 11 index with "," index ", new {id = 0, id2 = 11}, blank); @html.exe link (" index "," index ", new {id =" no ", id2 = "No"}, blank); // Removes the "/" URL with the existing root values ​​  

My problem is that it's a dirty way of doing and I'm sure Is that there is a better way I do not want to be sure that it was not possible for "none" id or id2 as it seems that there could be a danger of using the "justAction" route by mistake.

Is it better to use MVC3 to remove existing path values ​​and stop them from appearing as URL query string?

Strictly speaking, you use a barrier to stop using "no" Can :

  routes. New {id = new NotNoneConstraint (), ID2 ("justID", "{controller} / {action} / {id} / {id2}", new {controller = "home", verb = "index"} = New NotNoneConstraint ());   

and NotNoneConstraint class:

  Public category NotNoneConstraint: IRTE Consultant {Public Boole Match (HTTPTPTBTBTTPTXT, Route Route, String parameter name, route evaluation value, router directional route direction) {return value. Cantankake (parameter name) & amp; Amp; Value [parameterName]! = "No"; }}   

If this parameter is equal to "no" , the routes will be effectively prevented by matching. The parameter with NotNoneConstraint will also be mandatory for the route to match the given URL.


Now, if your ID argument is only numeric, then it will be much more elegant to handle the constraints easier to use with a regular expression:

  . New {id = @ "\ d +", MapRoute ("justID", "{controller} / {action} / {id} / {id2}", new {controller = "home", action = "index"}, Id2 = @ "\ d +");    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -