简体   繁体   中英

Why href URLs in IIS show "HTTP Error 404.0 - Not Found" error? (ASP.NET MVC C#)

I published my ASP.NET MVC Web App to IIS, and it does open my first two pages (Login and Home), However in my navbar i have multiple menus (links <a> ) with href.(eg href="/Users/Index")

When i open my app in browser, it shows: http://localhost/AppName/Home

when i click a menu item on my navbar it doesn't add the appname, and doesn't render my view: http://localhost/User/Index

and sends a HTTP Error 404.0 - Not Found

Obviously if i add the AppName manually it does open the view.

This doesn't happen in VS IDE but when i publish it to IIS, the routing does not seem to work.

Any ideas??

RouteConfig.cs

  public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Link on Navbar

 <a href="/User/Index" class="dropdown-toggle"> Users</a>

My web.config does have:

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Update!

Following VeNoMiS's advice, i inserted Url.Action to all my navbar menu items, spliting the URL i got from my database into each parameter (view, controller, area).

All routing from within IIS worked perfectly.

<a href="@Url.Action(Url.Split('/')[2], Url.Split('/')[1], new { Area = Url.Split('/')[3] })"><span class="@Icon" title="@Icon"></span> @Name</a>

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM