Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to improve site weight by using Routing feature in ASP.NET MVC3

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use the Routing feature in ASP.NET MVC3 to improve the weight of your site. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Text

For SEO, an address corresponding to a unique independent content is an important step to ensure the best weight, so we need to make sure that the content corresponding to each URL address is not duplicated (that is, a different Action for MVC), but the ASP.NET MVC3 program is problematic by default. For example, the Action method HomtController.Index can be mapped to multiple addresses, for example:

1. Http://abc.com (default)

2. Http://abc.com/ (end of slash)

3. Http://abc.com/Home with Controller

4. Http://abc.com/Home/Action with Controller and Action

5. Http://abc.com/home/Action (different situations)

Wait

One way to solve this problem is to use IIE URL Rewrite Extension, but it is quite complex to configure, so here we will take advantage of MVC's own features to solve this problem (by registering the global filter) and add the following class:

Public class RemoveDuplicateContentAttribute: ActionFilterAttribute {public override void OnActionExecuting (ActionExecutingContext filterContext) {var routes = RouteTable.Routes; var requestContext = filterContext.RequestContext; var routeData = requestContext.RouteData; var dataTokens = routeData.DataTokens; if (dataTokens ["area"] = = null) dataTokens.Add ("area", ""); var vpd = routes.GetVirtualPathForArea (requestContext, routeData.Values); if (vpd! = null) {var virtualPath = vpd.VirtualPath.ToLower () Var request = requestContext.HttpContext.Request; if (! string.Equals (virtualPath, request.Path)) {filterContext.Result = new RedirectResult (virtualPath + request.Url.Query, true);}} base.OnActionExecuting (filterContext);}}

Then register filter in Global:

Public static void RegisterGlobalFilters (GlobalFilterCollection filters) {filters.Add (new HandleErrorAttribute ()); filters.Add (new RemoveDuplicateContentAttribute ();}

Let's explain:

First, RemoveDuplicateContent filter gets my RequestContext and RouteData, and then determines that if you don't currently use Area, add a null value to DataToken, which is very important, because if you don't add it, you will make an error if you use the Area feature later.

Next, the filter gets the virtual path through RouteData, and then toLower converts it to lowercase.

Then, compare it with the path of the current request, and if it is inconsistent, redirect it to the lowercase virtual path, so that the search causes you to recognize that when writing multiple requests is actually corresponding to the real address of your virtual path, that is, the only address that corresponds to that action, so that one address corresponds to one content.

Thank you for reading! On "how to use Routing features in ASP.NET MVC3 to improve site weight" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report