Skip to main content

Url.Action Extension Method written in C# to handle multi-language Routes in ASP.NET MVC

As anticipated here – and also by popular demand – we’re publishing an Extension Method sample that will enhance the Url.Action helper method in any Razor View allowing the developer to specify a CultureInfo object – or even a null one, to use the CultureInfo.CurrentCulture – that will be used to build a multi-language URL. Needless to say, such URL will only work if we configured a multi-language aware Route within our application such as the one we mentioned in this post.

Read More

Html.ActionLink Extension Method written in C# to handle multi-language Routes in ASP.NET MVC

As anticipated here – and also by popular demand – we’re publishing an Extension Method sample that will enhance the Html.ActionLink helper method in any Razor View allowing the developer to specify a CultureInfo object – or even a null one, to use the CultureInfo.CurrentCulture – that will be used to build a multi-language ActionLink (i.e. an ActionLink with a multi-language URL). Needless to say, such URL will only work if we configured a multi-language aware Route within our application such as the one we mentioned in this post.

Read More

Setup a multi-language website using ASP.NET MVC

Introduction

Since the first release of the .NET framework developers are given the chance to easily configure any kind of project – be it a Website, a Web Application, a Windows Forms or XPF/XAML client and such – in order to support multiple languages. This can be achieved using the well-known Resource Files (.resx) approach. We won’t explain them here (if you’re interested, read the official walkthrough), but we’ll remember a couple key concepts. A Resource Files is basically a key/value array of content resources (mostly images and text) for each supported language. All the developers have to do is to create a .resx file for the main language (let’s say english) and another one for each of these languages using the same name plus the ISO 639-1 two-letters language code of the language itself, i.e.:

  • Global.resx file to store text and images for english, assuming it’ll be our default & fallback language.
  • Global.it.resx file to store text and images for italian language
  • Global.de.resx file to store text and images for german language

and so on. Once we did that, we’ll only have to write our code using the key specified in these files instead of the actual content (if you don’t know how, read the walkthrough above): ASP.NET will look up the keys in our Resource Files, starting from the one with the Localization closest to the one set for the current thread and then going backwards until it founds something to show.

Kickass feature, indeed: let’s see how we can use it to build our very own multi-language MVC ASP.NET Web Application.

Read More

HTTP Basic Authentication with ASP.NET MVC using a custom ActionFilter

A common-case scenario during the development of an ASP.NET MVC web application is the need to restrict the access to some web resources to authenticated users only. If our application features an authentication system based on ASP.NET Membership framework (like the ASP.NET Membership Provider or the updated ASP.NET Identity) you can easily fullfill the task by using the AuthorizeAttribute provided in the System.Web.Mvc namespace to only allow specific Users and/or Roles for a whole Controller and/or for a specific ActionResult:

This simple yet effective approach requires the user to authenticate himself using a login mechanism (usually a login form page or view) and that’s why it’s not really useful when you need to put togheter a RESTful interface and/or a WebService  of any kind. The best way to deal with these things is to adopt one of the many authentication mechanisms supported by the HTTP protocol: Basic, Digest, NTLM just to mention some. The most used, yet also the easiest one to blend into a MVC pattern, it’s definitely the Basic Authentication.

To implement it in your application just add the following ActionFilter custom attribute to your project:

As you can see the filter checks for the presence of the Authorization request field and acts accordingly:

  • If it’s there, it checks the given username and password with those expected by the server application.
  • If it’s not there, it builds a standard 401 Http Response to tell the client that we need some credentials to access that specific resource. This kind of response will make most web browsers prompt the user with an “insert username and password” standard popup form.

The above implementation requires the developer to manually insert the username and password as ActionFilter required parameters but can be easily extended to make it support any authorization mechanism (MembershipProvider, ASP.NET Identity, custom userbase on an external DBMS or file, etc.) by removing the custom constructor and modifying the OnActionExecuting method IF block accordingly.

The BasicAuthenticationActionFilter attribute is just as versatile as the previously mentioned Authorize attribute, meaning that it can be used to put under Basic Authentication a whole controller or a specific ActionResult:

 

Close