1

NOTE: This is not a duplicate of another question as my first page works fine, it is other pages/actions that are not working.

I've ready many posts so far and nothing comes close. This works just fine when run locally on my development box. After I copy to the server, I see the problem. My default route works fine, I get the expected index page.

public ActionResult Index()
{
    return View();
}

My RouteConfig contains:

routes.MapRoute(
    name: "AnnualFees",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "AnnualFees", action = "Index", id = UrlParameter.Optional }
);

Problems arise when I want to reach anything other than Index. For example, this action causes a 404 not found error:

public ActionResult renderForm()
{
    return PartialView("_formPanel");
}

Again, works as it should on my local dev box. But on the server I get Requested URL: /AnnualFees/renderForm 404 error The resource cannot be found.


UPDATE

Ok, doing some more research and trial and error, I discovered something and have a bit more to add.

This app is running under a current website in IIS, where I created a Virtual Folder/application under the website root. If I navigate to www.mysite.com/AnnualFees I get the first page of my MVC app as expected. However, the only way I can get to any other action in my AnnualFeesController, I have to double up the controller name like www.mysite.com/AnnualFees/AnnualFees/renderForm which works but is ugly and not quite right. How can I get rid of the redundancy?

Connie DeCinko
  • 996
  • 5
  • 19
  • 39
  • Possible duplicate of [Getting 404 error on MVC web-site](http://stackoverflow.com/questions/5068770/getting-404-error-on-mvc-web-site) – James P Nov 08 '16 at 22:59
  • Well, you need to setup IIS properly. At the moment "www.mysite.com/AnnualFees" references your application, and uses all defaults in your route, including controller. "www.mysite.com/AnnualFees/AnnualFees" would yield the same. Setup you need can be found [here](http://serverfault.com/questions/68497/how-do-you-configure-iis-7-to-use-a-subdirectory-as-the-default-document) – Andrei Nov 08 '16 at 23:55
  • @Andrei not sure what you mean by setup IIS properly. This app is needing to run under an existing website, thus must run within the /AnnualFees subfolder of the site. I need /AnnualFees to be the root of the app, but it still needs access to files in other folders of the site. – Connie DeCinko Nov 09 '16 at 19:29
  • @ConnieDeCinko, oh, ok, i misunderstood what you are looking for. Having your web site under one of the IIS folder is ok, see an answer below about how to avoid the duplication – Andrei Nov 10 '16 at 00:07
  • This will also happen if you have multiple mapped routes with the default one generated by Visual Studio as the first one. That default route has to be registered last. – Roy Oliver Feb 27 '19 at 19:40

1 Answers1

1

So the problem, as I noted in the comment, is that you have a folder, and under it goes the route. If you do not provide any parts of the route, just calling

www.mysite.com/AnnualFees

this uses all defaults as specified in your route config, and you get the default page. However if you type

www.mysite.com/AnnualFees/ActionName

MVC sees that as controller ActionName and no action no id provided. This does not exist in your app, thus the error.

The root problem here is that websites are not supposed to live under folders, they are supposed to live under top domains. That is, it is not expected that site root URL is www.mysite.com/AnnualFees, it is supposed to be just www.mysite.com. But even that would be fine if you did not have your main controller and IIS folder with the same names, producing unwanted duplication.

You can however change you route to make AnnualFees a default controller. Simply remove the controller part like so:

routes.MapRoute(
    name: "AnnualFees",
    url: "{action}/{id}",
    defaults: new { controller = "AnnualFees", action = "Index", id = UrlParameter.Optional }
);

Now you should be able to use

www.mysite.com/AnnualFees/ActionName

Again, note that in the above URL "AnnualFees" is not a controller name, it is in fact no visible to MVC app at all.

There is however a caveat. Imagine you need to add another controller. Now the default and only route would not work with it. The key is to provide a separate route for this controller, with hardcoded first part

routes.MapRoute(
    name: "NewControllerRoute",
    url: "NewControllerName/{action}/{id}",
    defaults: new { controller = "NewController", action = "Index", id = UrlParameter.Optional }
);

Make sure to put this route before the default one, so that all requests to this controller are routed correctly, and all other requests go to "AnnualFees".

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Ok, now I can see the forest for the trees. It makes more sense now... I am calling a method that is in a controller that is in a subfolder. So my seeing AnnualFees twice was correct, once for the folder name, and then again for the controller name. I was also seeing issues due to if the page was called with a trailing slash or not. So, for now all is sorted out and working as expected. – Connie DeCinko Nov 10 '16 at 21:02