6

I have a requirement to create a Get method which takes the following parameter names in the URL:

ms-scale ms-contrast ms-lang

As you can see, all the names have a dash in them which is not possible in C#. How can I map my method to these parameter names?

public HttpResponseMessage Get(int scale, string contrast string lang)
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

2 Answers2

9

Use FromUriAttribute

public HttpResponseMessage Get([FromUri(Name = "ms-scale")]int scale, [FromUri(Name = "ms-contrast")]string contrast, [FromUri(Name = "ms-lang")]string lang)
phdesign
  • 2,019
  • 21
  • 18
  • This should be accepted answer. No need to write custom binders or whatever - just out-of-box solution. – Yura Sep 16 '15 at 11:25
4

I was asked this before somewhere else and found this answer:

Using a dash (-) in ASP.MVC parameters

Updated

In order to get this working with Web API we need to modify it a bit.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class BindParameterAttribute : ActionFilterAttribute
{
    public string ViewParameterName { get; set; }
    public string ActionParameterName { get; set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var viewParameter = actionContext.Request.RequestUri.ParseQueryString()[ViewParameterName];
        if (!string.IsNullOrWhiteSpace(viewParameter))
            actionContext.ActionArguments[ActionParameterName] = viewParameter;

        base.OnActionExecuting(actionContext);
    }
}

And how to use it:

[BindParameter(ActionParameterName = "customData", ViewParameterName = "custom-data")]
public string Get(string customData) {}

Please note, that this only works if your data comes from the uri, not body. How to make it work with POST data, I'm not really sure of at the moment.

Community
  • 1
  • 1
Kordonme
  • 2,314
  • 3
  • 20
  • 32
  • I set a breakpoint in the method and it does not seem to be breaking. – Muhammad Rehan Saeed Sep 06 '12 at 11:36
  • [ParameterName(ActionParameterName = "scale", ViewParameterName = "ms-scale")] [ParameterName(ActionParameterName = "contrast", ViewParameterName = "ms-contrast")] [ParameterName(ActionParameterName = "language", ViewParameterName = "ms-lang")] public HttpResponseMessage Get( int scale = 100, string contrast = "standard", string language = "en-us") – Muhammad Rehan Saeed Sep 06 '12 at 11:37
  • 1
    For Web API you should be using the System.Web.Http.Filters namespace. Are you using that namespace? I'll try and make an example. – Kordonme Sep 06 '12 at 12:59
  • If I use System.Web.Http.Filters.ActionFilterAttribute where does ActionExecutingContext come from? It keeps trying to reference System.Web.Mvc.ActionExecutingContext which is incorrect. – Muhammad Rehan Saeed Sep 06 '12 at 13:06
  • That does not seem to work if the parameter is an int. Works for String's though. – Muhammad Rehan Saeed Sep 06 '12 at 14:02
  • How about: Type type = actionContext.ActionArguments[this.ActionParameterName].GetType(); actionContext.ActionArguments[this.ActionParameterName] = Convert.ChangeType(viewParameter, type); – Muhammad Rehan Saeed Sep 06 '12 at 14:05