Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Monday, 4 November 2013

Subdomain with ASP.NET MVC using Attribute Routing

ASP.NET MVC Routing allows defining various mappings and recently I have looked at an interesting framework known as Attribute Routing.

Using Attribute Routing setting up a subdomain for your website that uses ASP.NET MVC is very simple. Now consider you have a website www.csharptechies.com and you would want to define subdomains such as blog.csharptechies.com, projects.csharptechies.com, labels.csharptechies.com etc.... Each of these subdomain can be a different project (any technology such as webforms, MVC etc) by themselves or can be an area defined in an ASP.NET MVC project.

Lets consider we are going to follow the approach of having areas inside ASP.NET MVC project. Here are the list of steps to achieve this.

1. Create your ASP.NET MVC project with all the content and publish to your IIS. You should now be able to access your website using http://localhost/csharptechies or based on your machine http://saifpc.localhost.com/csharptechies. You could define hosts file to include the references.

I have added following to "hosts" file under C:\Windows\System32\Drivers\etc with data as

127.0.0.1 www.csharptechies.com

Now website can be accessed as www.csharptechies.com\csharptechies(ie.. http://localhost/csharptechies)

2. Lets code for subdomain routing. First add references for attribute routing via nuget using "Install-Package AttributeRouting"

3. Create an area with the name "Blog".

4. Add a new folder "Blog" under "Areas\Blog\Views". create a new view "articles" under "Areas\Blog\Views\Blog"

5. Add a controller BlogController.cs with following code under "Areas\Blog\Controllers"
 [RouteArea("Blog", Subdomain = "blog")]
    public class BlogController : Controller
    {
        //
        // GET: /articles/
        [GET("")]
        public ActionResult articles()
        {
            return View();
        }
    }
6. Update "AttributeRoutingConfig.cs" to add routing to blog
public static class AttributeRoutingConfig
 {
  public static void RegisterRoutes(RouteCollection routes) 
  {   
routes.MapAttributeRoutes(config => config.AddRoutesFromController());
  }

        public static void Start() 
  {
            RegisterRoutes(RouteTable.Routes);
        }
    }

7. Now add new entry to hosts file

127.0.0.1 blog.csharptechies.com

8. Publish latest version of the project and using IE browse to the website as

http://blog.csharptechies.com/csharptechies

This will show the view as articles, since BlogController now understands the attribute definition of subdomain as "blog" it will route the url to blog instead of standard csharptechies website. You could also look at possible routing via www.csharptechies.com\csharptechies\routes.axd

Technology is just a tool. In terms of getting the kids working together and motivating them, the teacher is the most important.
Bill Gates
Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers