6

Let's say I have a most basic controller

public class HomeController : Controller
{
    public ActionResult Index(string id, string language)
    {
        return View();
    }
}

Which takes in 2 parameters. However there is one requirement that the client which calls the action method should pass id value from URL but language value from http header. It means the url should be /Home/Index/12345 and meanwhile the calling client will set a Http Header value language : en.

How shall I set the routing in MVC5 or MVC6 to achieve the requirement?

Please don't provide samples from Web Api.

Thanks

3
  • 1
    Have you checked the FromHeaderAttribute? Try adding it to the language parameter as in public ActionResult Index(string id, [FromHeader] string language) Commented Jul 16, 2015 at 16:45
  • I never notice this attribute. But when I Google the attribute I barely see any further information other than the github page you gave me. Commented Jul 16, 2015 at 16:52
  • That "github page" is the official Microsoft repo for the new MVC framework :). But yes, documentation is scarce at the moment, hopefully they will improve it over time Commented Jul 16, 2015 at 16:54

2 Answers 2

10

There is an attribute FromHeaderAttribute. From its documentation:

Specifies that a parameter or property should be bound using the request headers.

You should be able to add it to the language parameter of your controller. By default it will look for a header with the same name than the parameter, but it also has a name parameter that can be used to specify a different name, for example:

public ActionResult Index(string id, [FromHeader(Name="Accept-Language")]string language)
{
    return View();
}

You can also have a look to the test site ModelBindingWebSite located in the github MVC repo. Check the controller named FromHeader_BlogController.

PS Looking at the source code of the HeaderModelBinder it seems this can be used for binding strings and arrays (assuming the header has a comma separated list of values)

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

7 Comments

Great for the sample, I will give it and let you know the result.
I checked the source code of the attribute, it seems that it is difficult to extend. For example what about the http header is Accepted-Language, I cannot have a parameter name like it. So preferably I should be able to use [FromHeader("Accepted-Language")] to specify which http header value to bind from.
The attribute has a "Name" parameter, have you tried [FromHeader(Name = "Accepted-Language")]? In the sampes website they have a similar case public object BindToStringParameterWithCustomName([FromHeader(Name = "tId")] string transactionId)
Great, how can I miss it!!
The header-name is Accept-Language, not Accepted-Language. Otherwise correct.
|
0

As Daniel J.G. said, you can bind a Controller action parameter with FromHeaderAttribute. But keep in mind that a Controller can access Request.Headers directly. It might be nicer to leave off the Controller's language parameter and access the value as an enumeration via a property:

  public enum LanguageType
  {
     Unknown = -1,
     English,
     Spanish,
     German,
     Chinese
  }

  public LanguageType Language
  {
     get
     {
        string langStr = Request.Headers["Accept-Language"];
        switch (langStr.ToLower())
        {
           case "english":
              return LanguageType.English;
           case "spanish":
              return LanguageType.Spanish;
           case "german":
              return LanguageType.German;
           case "chinese":
              return LanguageType.Chinese;
           default:
              return LanguageType.Unknown;
        }
     }
  }

2 Comments

The header-name is Accept-Language, not Accepted-Language; Please don't copy-paste other people's mistakes without thinking.
@StefanSteiger thanks for your comment! The header spelling is now corrected.

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.