0

I've searched a lot but didn't find any solution. So here is my case:

I have database model UrlRedirect

public class UrlRedirect : AuditInfo
{
    [Key]
    public int Id { get; set; }

    public string OldUrl { get; set; }

    public string NewUrl { get; set; }
}

As you may assume I am trying to save URL mapping between OldUrl and NewUrl.

I want to internally change the OldUrl path of the request to the NewUrl and then run all defined routes as they will run if the user is opening NewUrl directly.

The redirect should be server side URL rewrite and user should see the old URL in their browser

3
  • What kind of "redirect" are you expecting? 301? 302? or simply server side url rewrite that user still see the old URL in their browser? Commented Jan 10, 2014 at 13:23
  • I want server side url rewrite that user still see the old URL in their browser :) Commented Jan 10, 2014 at 13:24
  • 1
    Generally this should be done by Routing in MVC, since you want to route url to url that might be slightly complicated and possibly you need to change your data structure. A raw way is to HttpContext.Current.RewritePath() in Application_BeginRequest in Global.asax. Let me know if you need some code example for either way. Commented Jan 10, 2014 at 13:29

3 Answers 3

3

In Global.asax you have some events that are executed in the web application context. (for sample: Start_Application, End_Application, etc).

In your case, you could use the BeginRequest event, where every request made to your web application is executed. In this event you could check the URL and try to redirect it using the default of htpp protocols, such as 301 Moved Permanently status code. For sample, in the Global.asax file, add the code bellow:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    // get the current url
    string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower(); 

    // here, you could create a method to get the UrlRedirect object by the OldUrl.
    var urlRedirect = GetUrlRedirect(currentUrl);

    // check if the urlRedirect object was found in dataBase or any where you save it
    if (urlRedirect != null)
    {
        // redirect to new URL.
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", urlRedirect.NewUrl);
        Response.End();
    }
}

In the sample, GetUrlRedirect(string) method should check it in a database, xml file, cache, or anywhere you save the UrlRedirect objects.

To understand more about how asp.net core applications life cycles works, read this article.

Sign up to request clarification or add additional context in comments.

Comments

1

If you really want to redirect within the server you may use one of the following:

  • HttpServerUtility.TransferRequest
  • HttpServerUtility.Transfer
  • HttpServerUtility.Execute
  • HttpContext.RewritePath

You can read more about these options, where a similar problem is posed here.

Consider the impact of your choice on the request serving performance. IMHO you should try your best to utilize MVC infrastructure of Routing, or just fall back to simple redirections (even permanent for speed) as [user:316799] wrote when you compute new urls in business layer or map from db.

Comments

1

This is my final solution that works like a charm. Thanks to @WeTTTT for the idea.

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        // ...
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        this.ApplyCustomUrlRedirects(new UowData(), HttpContext.Current);
    }

    private void ApplyCustomUrlRedirects(IUowData data, HttpContext context)
    {
        var currentUrl = context.Request.Path;
        var url = data.UrlRedirects.All().FirstOrDefault(x => x.OldUrl == currentUrl);
        if (url != null)
        {
            context.RewritePath(url.NewUrl);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.