html5mode gives 404 not found

家住魔仙堡 提交于 2019-12-13 07:41:39

问题


I'm trying to remove the hashtag from the URL in my AngularJS app. I'm using Web API 2.0 to retrieve some data. This is what I've done:

In my app.js

app.config(function($routeProvider, $locationProvider) {
        $routeProvider
            .when("/products", {
                templateUrl: "app/products/productListView.html"
            })
            .when("/products/:id", {
                templateUrl: "app/products/productInfoView.html" 
            })
            .otherwise({ redirectTo: "/products" });

        $locationProvider.html5Mode(true);
});

I've also added <base href="/"> to my index.html.

It works fine when I go to http://localhost:49767/ and navigate from there using links.

For example, clicking a product takes me to http://localhost:49767/products/5 and loads the information just fine. However, if I go directly to http://localhost:49767/products or directly to a specific product I get 404 not found error from IIS.

What am I missing?


回答1:


You have to configure your server to work with html5mode.

ASP.Net C# Rewrites

In Global.asax

private const string ROOT_DOCUMENT = "/default.aspx";

protected void Application_BeginRequest( Object sender, EventArgs e )
{
    string url = Request.Url.LocalPath;
    if ( !System.IO.File.Exists( Context.Server.MapPath( url ) ) )
        Context.RewritePath( ROOT_DOCUMENT );
}

See this link for more information




回答2:


In my app, my way is :

delete $locationProvider.html5Mode(true);

and <a href="#/products/1">1</a><a href="#/products/2">2</a>



来源:https://stackoverflow.com/questions/31424507/html5mode-gives-404-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!