ASP.NET MVC current request url (ajax call issue)

ajax, asp.net, asp.net-mvc

Solution

Ok, i got this. We can use Request.UrlReferrer property for ajax requests to retrieve proper url, like this:

public ActionResult MyActionMethod()
{
            if (Request.IsAjaxRequest())
                ViewBag.ReturnUrl = HttpContext.Request.UrlReferrer.LocalPath;
            else
                ViewBag.ReturnUrl = HttpContext.Request.RawUrl;

            return View();
}

Problem

I use popular technique "ReturnUrl". I pass current page url to server, do some staff and then i redirect user back to this url. For example, user posts comment and then get back to this url. ``` @using (Html.BeginForm("AddUserComment", "Home", new { returnUrl = HttpContext.Current.Request.RawUrl }, FormMethod.Post, new { enctype = "multipart/form-data" })) { ... } ``` BUT, when i load this form through ajax call, like this: ``` $.ajax({ type: 'GET', url: '/Home/ShowUserCommentsBlock/', data: { entityType: entityType, entityId: entityId }, cache: false, ... }); ``` `HttpContext.Current.Request.RawUrl` returns ajax request url `"/Home/ShowUserCommentsBlock?entityType=..."`, but i need current page url, where ajax request is called. What should I use instead of `HttpContext` object?

Original source