I have a controller with 2 actions that each return the response from the Index action after setting a given parameter.
public ActionResult Index(int? pageType)
{
// Do something based on the pageType
return View();
}
public ActionResult Guides()
{
return Index(1);
}
public ActionResult Discussions()
{
return Index(2);
}
The reason I do this is to make the Url a bit cleaner without messing too much with routing (setting the route to accept the page type as string parameter would confuse things with mapping to the controller actions, and I wanted the url to show the page type)
Just to confuse things, I dynamically check for a matching view based on the pageType parameter and use that, otherwise I stick to the default ‘Index’ view. I’m using the standard route mapping to catch this.
As is often the case, this all works wonderfully locally. The problem comes when accessing the Url on the web server. When I navigate to the ‘Guides’ action it all works fine, however navigating to the ‘Discussions’ action returns a 404. The strange thing is that accessing the same Url in a browser on the server itself works fine!
It’s running on Windows Server 2008 R2 (IIS7) with application pool set to Integrated. It’s also MVC 3 on .Net 4.
Has anyone seen anything like this before or have any idea how to diagnose? I’m not sure where to go from here…
EDIT:
OK so for brevity I missed out some things that I didn’t think were relevant, how wrong I was…
In addition to the pageType parameter, there’s also an optional category. This checks that the category exists in the DB and adds it to the view model.
Within the View, there is a call to @Html.RenderAction which sends the category Id to another action method. I’m not sure why I did it this way instead of just calling @Html.Partial passing in the model but hey. Anyway if the category was not found, this action method returned a HttpNotFound result, hence the 404.
I just changed the ‘child’ action method to return the partial view using an empty object as the model (a new empty list in my case) and it fixed the issue.
It took a bit of digging but I got there in the end, in future I’ll be sure to use RenderAction only when I really need to do additional action processing that can’t be done in the initial controller action.
Of course, this doesn’t help explain why it worked when accessing locally on the server. I guess that must be something to do with the HttpNotFound result.
So, just to resolve the question. In the parent View I had a call to
@Html.RenderActionwhich in turn executed the code below:This was returning a
HttpNotFound()ActionResult which in turn resulted in a 404 for the parent view.I changed the code to use an empty list as the model if not found and that solved the problem.