Adding to RouteValueDictionary of the Url.Action at runtime - asp.net-mvc-3

I have a string property named Country which is avaialble in view through all viewmodels.
I want to add this property to RouteValueDictionary of Url.Action so for now I have to change every
Url.Action to
Url.Action("action","controller", new RouteValueDictionary{new
{Country=Country})).
I am looking for clean solution, adding it to RouteValueDictionary at runtime
instead of changing the Url.Action everywhere
Please advice.
Thanks

Try it just as (using the anonymous object only):
Url.Action("action","controller", new {Country=Country} )

Related

MVC 4 WebAPI - Is there a way to get a dictionary of values passed to a POST action?

I am able to successfully retrieve an object via POST action.
I am trying to build a proof of concept for dynamic treatment of form values posted, so that I do not know at design time what those names or values will be.
Is there a particular type of binder or formatter etc. that should be added after [FromBody] attribute in the post action to achieve this?
Essentially I need a name-value-pair. I tried KeyValuePair and dynamic. Dynamic returns an object. I don't know how to get the key names and values out of it.
This should do what you are looking for.
public HttpResponseMessage Post(FormDataCollection collection) {
...
}

WebApi ApiExplorer ApiExplorerSettingsAttribute and IgnoreApi for Action Parameters

You can decorate a controller or action method with the ApiExplorerSettingsAttribute setting the IgnoreApi property to NOT generate help info. If you try to apply the same to an action method's attribute, you get an error:
public HttpResponseMessage Post([ApiExplorerSettings(IgnoreApi = true)]HttpRequestMessage request, ... )
Error 2 Attribute 'ApiExplorerSettings' is not valid on this declaration type. It is only valid on 'class, method' declarations.
A common convention for keeping your controller actions testable is to accept an HttpRequestMessage parameter, but this is an implementation detail, not something your API consumers should know about.
How can I prevent the ApiExplorer from including this parameter when it generates the help page?
For clarity...by this "if you try to apply the same to an action method's attribute", did you mean you were trying to apply to the parameter?
-A quick fix would be is to add an additional check to the existing CancellationToken check that we have in this file: \Areas\HelpPage\Views\Help\DisplayTemplates\Parameters.cshtml
// Don't show CancellationToken because it's a special parameter
if (!typeof(CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType))
{
-Also, you could avoid having HttpRequestMessage as a parameter on action as you can get the current request from the Request property on the controller, but you wanted it as a parameter for testing...is it?

MVC3, turn off field validation for a field not in a ViewModel

I have a Form in an MVC3 project. One of my input fields should accept HTML. Unfortunately I cannot have a ViewModel which this value maps to. The Field is autogenerated and read in automatically. I am getting the following error.
A potentially dangerous Request.Form value was detected from the client
Since there is no viewmodel, I cannot apply the [AllowHTML] attribute. Does anyone know a workaround that does not involve disabling validation for the entire page?
Thank You
Additional Information:
I can access the unvalidated value by doing the following:
using System.Web.WebPages;
using System.Web.Helpers;
.....Inside Controller....
string value = Request.Unvalidated("input-40");
The problem now is that the Request.Params collection throws an exception. I would like to access all the other values and have them be validated...just not that one. Is there a way for me to validate the other fields either explicitly or access a validated collection.
The following would be fine
string value = System.Web.Something.ValidateInput(Request.Unvalidated("input-41"));
Unfortunately I don't know where/if this method exists
You can try the ValidateInput(false) attribute:
[ValidateInput(false)]
public ActionResult YourAction(FormCollection yourCollection)
{
// your stuff
}
Use ValidateInput attribute for your action method. Seems to be unsafe but should work, cannot test it now.

localized routes solution

I built a french/english app and I would like to use the same controller/view for both language but to have a different route that is map to the current language. Let say I have website.com/Account/Register that return to my Account controller and Register action, I would love to have a route that is website.com/Comptes/Inscription. I know that I can add a custom route in the RegisterRoute section like so :
routes.MapRoute(
"AccountFr", // Route name
"comptes/inscription", // URL with parameters
new { controller = "Account", action = "Register" } // Parameter defaults
);
But it will need a lot of [boring] code to write all the possibles routes and also, I think it won't work when I will use T4MVC as #Url.Action(MVC.Account.Register()) will return /Account/Register no mater if I'm in french or in english.
Anyone as suggestions/ideas for this problem?
Thanks!
EDIT
Since it does not seem to have a good solution using T4MVC does anyone have an other good solution?
Unfortunately, this won't easily work with T4MVC. The root of the problem is that when going through T4MVC, you can't pick a specific route. Instead, the route gets selected based on the Controller, action and parameters.

MVC3 razor remote validation - Controller argument is always empty

I can call the controller but the argument (string) is always null.
All the examples I have found name the controller argument the same as the property we are validating remotely, sounds good/easy, but if you look at fiddler what is really being passed in is the name attribute from the input statement. Well that is problematic in that it is a subscripted name something like Person.EMailAddresses[0].Address, well I can't name my controller parameter like that.
So how do I get around this? There must be a way to specify the controllers parameter name in the remote() attribute?
It cannot be done using the default RemoteAttribute. This is a link to an example I posted of a reusable remote validation attribute, where you can specify the name of the controller, action and the name of the variable used to pass the value to the action.

Resources