Delete 'WWW' in ASP.NET MVC 1.0

I am trying to make the domain name not use "www". I want to redirect the user if I try. I saw little in the MVC solution. Is there anyway to use the routing built into MVC, or what are the best solutions.

thank

+2
source share
4 answers

If you have control over the server, you must configure a virtual directory that accepts requests to "www.example.com" and redirects (301) them to "example.com"

While this is possible in ASP.NET MVC, redirecting this kind is not an ASP task.

On IIS: Virtual directory

On Apache:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect permanent / http://example.com/
</VirtualHost>

IIS and Apache settings will preserve the core URL.

+1

ActionFilter, MVC-, .

public class RemoveWwwFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var req = filterContext.HttpContext.Request;
        var res = filterContext.HttpContext.Response;


        var host = req.Uri.Host.ToLower();
        if (host.StartsWith("www.")) {
            var builder = new UriBuilder(req.Url) {
                Host = host.Substring(4);
            };
            res.Redirect(builder.Uri.ToString());
        }
        base.OnActionExecuting(filterContext);
    }
}

ActionFilter , .

Action Filters . MSDN.

[RemoveWwwFilterAttribute]
public class MyBaseController : Controller
+4

, , . , IIS7. , DiscountASP.NET URL Rewrite, IIS.

, URL- www 301 , www. , .

.

+3

This is a more general configuration, as you can write it once in the Rewrite URL of the root IIS (not a specific application pool) and it will automatically apply to all your IIS sites without any dependency on your domain name.

IIS Remove WWW

+2
source

Source: https://habr.com/ru/post/1772707/


All Articles