简体   繁体   English

删除#! 从angular.js在ASP.Net Web API中显示

[英]Removing #! from angular.js Urls in ASP.Net Web API

So I am trying to remove the #! 因此,我试图删除#! in my angular site and transition to html5. 在我的有角网站并过渡到html5。 I've looked at other guides and I've implemented them with the locationProvider and base within the index file. 我看过其他指南,并使用locationProvider和索引文件中的base实施了它们。 The site works fine when you direct it through ng-href to a link but when I press refresh/directly input the url I get a 404 error. 当您通过ng-href将其定向到链接时,该站点工作正常,但是当我按刷新/直接输入URL时,出现404错误。 I was reading this has to do with the server side in which it does not know where to route when a 404 is hit since the .otherwise() is a hashbang function. 我读到的这与服务器端有关,因为.otherwise()是hashbang函数,因此服务器端不知道在命中404时要路由到哪里。

That being said, I looked at the WebApi config and found I already had directed a default routeTemplate if it were to hit a 404 as shown below. 话虽如此,我查看了WebApi的配置,发现如果已经命中了404,则我已经指示了默认的routeTemplate,如下所示。

// Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Any help would be appreciated 任何帮助,将不胜感激

As you correctly mentioned, the issue lies on the server side. 正如您正确提到的那样,问题出在服务器端。 When you refresh your browser, the AngularJS routing doesn't kick in. Instead the browser issues a request to the server. 刷新浏览器时,不会启动AngularJS路由。相反,浏览器向服务器发出请求。 The server doesn't find anything at the desired location, so it returns a 404. 服务器在所需位置找不到任何内容,因此返回404。

Your current server side route that you posted in your question just handles requests to your web api. 您在问题中发布的当前服务器端路由仅处理对Web api的请求。

In addition to the web api route you do have to add an mvc route that delivers your start page. 除了Web api路由之外,您还必须添加一个可传递起始页面的mvc路由。

Assuming that your start page is index.html, add the following routing to your server side route config 假设您的起始页是index.html,请将以下路由添加到服务器端路由配置中

//MVC
RouteTable.Routes.Ignore("api/{*url}"); // ignore api routes for mvc
RouteTable.Routes.MapPageRoute("NonAPIRoute", "{*url}", "~/index.html");

if you do already have MVC Routing in place (for example in a class RouteConfig in the App_Start folder), then your RouteConfig class should look similar to this 如果您已经有MVC路由(例如,在App_Start文件夹中的RouteConfig类中),那么RouteConfig类应类似于此

using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication1
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("api/{*pathInfo}"); // ignore api routes 
            routes.MapPageRoute("NonAPIRoute", "{*url}", "~/index.html");
        }
    }
}

Add a config to your project to enable HTML5 mode and remove prefix 将配置添加到项目中以启用HTML5模式并删除前缀

.config(['$locationProvider', function($locationProvider) {
    $locationProvider.hashPrefix(''); // by default '!'
     $locationProvider.html5Mode({
         enabled: true
     });
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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