How to Structure The Application in ASP.NET MVC Areas

How to Structure The Application in ASP.NET MVC Areas

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC hosting. This week I discovered a difficulty related to structuring an ASP.NET MVC web applications one advancement crew was dealing with. The things they have been trying to do was quite straightforward: to create a folder framework each having their particular subfolders for View/Controller/Scripts/CSS and so on. The appliance assets like JS/CSS etc. were not getting rendered properly. The difficulty was owing to NET.config file lying underneath the subfolder, which when moved to the Views folder below that subfolder things went good. The purpose of this post just isn’t to debate regarding the specifics of that difficulty and it is solution.But to discuss regarding how we will very easily framework our ASP.NET MVC Web application as per distinct modules, which is an clear need for just about any big application.

ASP.NET MVC follows the paradigm of “Convention Over Configuration” and default folder structure and naming conventions operates good to get a smaller sized software. But for relatively bigger a single there is a necessity to customise.The framework also offers adequate provisions for the same.You’ll be able to have your personal controller manufacturing facility to get custom methods to making the controller classes and custom see engine for finding the rendering the sights. But when the necessity would be to structure the applying to distinct subfolders as per modules or subsites I believe using “Area” in ASP.NET MVC will likely be useful to make a streamlined software.

You can add a area to some ASP.NET MVC undertaking in Visual Studio as shown beneath.

aspm

as1

Here I have added an area named “Sales”. As shown in the figure below a folder named “Areas” is created with a subfolder “Sales”. Under “Sales” we can see the following

  • The standard folder of Models/Views/Controllers
    • A Web.config under the Views folder. This contains the necessary entries for the RazorViewEngine to function properly
  • A class named SalesAreaRegistration.

res

The code (auto generated) for the SalesAreaRegistration class is shown below:

 System.Web.Mvc.AreaRegistration may be the summary base class use registering the places to the ASP.NET MVC Web Application. The method void RegisterArea(AreaRegistrationContext context) must be overriden to sign up the realm by providing the route mappings. The class System.Web.Mvc.AreaRegistrationContext encapsulates the mandatory information (like Routes) required to sign-up the area.

In Global.asax.cs Application_Start occasion we must RegisterAllAreas() technique as demonstrated under:

 The RegisterAllAreas method looks for all types deriving from AreaRegistration and invokes their RegisterArea method to register the Areas.

Now with the necessary infrastructure code in place I have added a HomeController and Index page for the “Sales” area as shown below.

e

I have to change the Route Registration for the HomeController to avoid conflicts and provide the namespace information as shown below:

 Now I will add a link to the Sales area by modifying the _Layout.cshtml as shown below:

Here I am navigating to the area “Sales” from the main application so I have to provide area information with routeValues. The following overload is being used in the code above:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);

Cheap ASP.NET MVC 6 Hosting – How To Improve Your Website’s SEO In 3 Easy Steps

Cheap ASP.NET MVC 6 Hosting – How To Improve Your Website’s SEO In 3 Easy Steps

CheapASPNETHostingReview.com | Best and Cheap ASP.NET MVC 6 hosting review and comparison. Did you know that the default route of ASP.NET MVC will produce multiple URL’s that display the same content? And as I am sure you are aware, duplicate content can cause all sorts of SEO problems. In this post, I will show you three steps to prevent duplicate content with ASP.NET MVC 6.

1. Use attribute routing instead of the default route

Attribute Routing is a new feature added to ASP.NET MVC 6 that allows you to manually define each route by placing an attribute on each action.

Why use attribute routing?

Create a new ASP.NET MVC 6 application, and you will find the following URL’s all show the home page:

  • http://localhost
  • http://localhost/home
  • http://localhost/home/

Now I know, that it is unlikely that the search engine will discover the duplicate URL’s, but it is better to make sure only one can be accessed, and this can be done by using attribute routing.

Also, in my experience I have found that using attribute routing instead of the default route makes it easier to debug routing issues.

How to enable attribute routing?

  1. Remove the default route from your RouteConfig. Or even better, replace the default route with a catch all that points to an action that displays 404 not found.
  2. Enable Attribute routing by calling the routes.MapMvcAttributeRoutes() method in your RouteConfig class.
  3. Manually add the route to each action. For example, assigning [Route("")] to the index action of the home controller will fix the issue above.

2. Add or remove the trailing forward slash from your URL’s

There is still a slight issue. For example, with the following attribute applied to the about page:

The about page can be accessed using the following URL’s:

  • http://localhost/about
  • http://localhost/about/

You can fix this by adding a rewrite rule to your Web.config file. For example, the following rule will remove the trailing forward slash from all URL’s:

Just remember to set the AppendTrailingSlash to false in the RouteConfig class so that the routing system generates URL’s that have no trailing forward slash.

3. Redirect none www to www

It’s best practice to have either the www redirect to the none www or the www redirect to the www. For example, on this site cheapaspnethostingreview.com redirects to www.cheapaspnethostingreview.com This prevents duplicate content.

The following example shows a rewrite rule that redirect the none www to the www subdomain.

The other good thing about the rule above, is it will redirect all domains to the primary domain. For example, if you host your site on ASPHostPortal. The ASPHostPortal.com domain will be redirected to your main domain, preventing more duplicate content.