Andy Evans
Andy Evans

Reputation: 7176

MVC3 and IIS6 RedirectToAction redirects to wrong URL

This is weird. I have a virtual directory setup for an MVC3 application called (for the sake of this question) I'll call 'foobar'. The full URL to this site is:

http://localservername.domainname.com/foobar

In my logon form, I have the following line that is supposed to redirect to the main/home page of the application after logon.

return RedirectToAction("Index", "Home");

However, when this line executes, I get redirected to the wrong location - so instead of redirecting me back to:

http://localservername.domainname.com/foobar

I get redirected back to:

http://localservername.domainname.com/foobar/foobar

Which of course gets me a resource not found error. Also, any links in my views seem to do the opposite - for example if I have a link like this:

<a href="/WidgetSearch">Widget Search</a>

I would expect the following URL to open:

http://localservername.domainname.com/foobar/WidgetSearch

Instead, I get redirected to

http://localservername.domainname.com/WidgetSearch

Which of course also gets me a resource not found error. I've never encountered this type of behaviour before. I've gone over the basic and advanced settings and created a new application pool. Fiddler also shows me that (of course) the URLs listed above return 404 responses.

Any suggestions would be greatly appreciated. Thanks!

Upvotes: 1

Views: 789

Answers (1)

Alconja
Alconja

Reputation: 14873

The first issue shouldn't really happen. RedirectToAction should take into account your virtual directory path (you haven't hard coded an extra /foobar into your route setup have you?).

The second problem has nothing to do with MVC, it's just that you're using a vanilla HTML link that is pointing directly to the root of the server (that's what saying /blah implies). You should change your link to use one of the MVC helper methods to generate the URL instead to make sure it adds the virtual directory for you. So one of the following (the first is probably the best way, unless you need to heavily customise what the anchor tag looks like):

@Html.ActionLink("Widget Search", "Index", "WidgetSearch")

or

<a href="@Url.Action("Index", "WidgetSearch")">Widget Search</a>

Upvotes: 1

Related Questions