0

I have a controller in an ASP.NET MVC 5 project that return the result of Redirect(url) where url is a string.

Essentially I have this:

string url = "calendar/addevent";
return Redirect(url);

The result in the browser is loading a URL like this: http://example.com/calendar/addevent#

This is fine in IE, but Chrome scrolls to the bottom of the page looking for an anchor because of the '#'.

Any idea on how to not include the '#' on the end of the URL?

Update 10/21/15

This is being returned from this method in the Account controller found in the template MVC 5 project using single-user authentication:

private ActionResult RedirectToLocal(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        return RedirectToAction("Index", "Home");
    }

The Url.IsLocalUrl is true and, if I debug and read the object returned from Redirect(returnUrl), the URL does not have the '#'. The browser is still somehow getting it though...

  • 1
    I cannot reproduce this, the `#` character is not in the `Location:` header in the Redirect response. What does your browser's F12 tool say about the `Location:` header? Do you have any scripts that are appending the `#` themselves? – Dai Oct 15 '15 at 21:19
  • 6
    Can you show us you're code? this is not normal behavior of MVC redirect. Are you using angularjs? – Ziv Weissman Oct 15 '15 at 21:24
  • This is happening in the RedirectToLocal method in the AccountController which is generated in a new project. The '#' character is never there when I debug, but the browser is always getting it... – James Stevens Oct 21 '15 at 19:18

3 Answers3

0

you can use Url.Action like below:

var url = Url.Action("ActionName" ,"ControllerName");
return Redirect(url);

Or

return RedirectToAction("ActionName" ,"ControllerName");
Iraj
  • 1,492
  • 5
  • 18
  • 42
0

I've found this is happening because of Google's OAuth response. Their query string on the response ends with '#'. Hence, the Redirect method in the Account controller returns a 302 response which, rightfully, persists the hash. Good analogy -> https://stackoverflow.com/a/5283528/2354959.

So the ASP.NET MVC controller is working correctly...

Community
  • 1
  • 1
-1

see sample Controller ActionResult

//Parameter can also be FormCollection
    public ActionResult eventview(string url){ 
           string[] splitURL=url.split('/');  //Considering URL - "calendar/addevent"
           return RedirectToAction(splitURL[1],splitURL[0]);
    }
Atul
  • 440
  • 8
  • 24