首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP.net Web中为角前端设置路由

在ASP.net Web中为角前端设置路由
EN

Stack Overflow用户
提问于 2018-09-13 14:11:45
回答 2查看 429关注 0票数 0

我正在尝试设置我当前的Web,以服务于一个Range6前端应用程序。

我的角项目位于Web下的'app‘目录中。

我可以导航到基本页面很好,并且所有的前端路由都很好。

我的开发是在:https://test2.localhost.com/app/

我必须将index.html中的基本位置设置为基本href="/app/“。

现在我的问题是直接导航到应用程序的子urls。例如:

https://test2.localhost.com/app/information/planets

我得到了404,这让我相信问题在Web路由中。

如果我要在https://test2.localhost.com/app/启动这个有角度的应用程序,我可以导航到url,但不能从浏览器的冷启动中导航。

我尝试过在web.config中重写几个规则,但似乎都失败了,并且阻止了我导航到https://test2.localhost.com/app

Web API正在IIS上运行。

当在nodeJs上运行前端时,路由工作得很好,我可以从冷开始导航到所有子urls。

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-13 16:03:59

假设您也有MVC路径,那么在您的route.config中尝试这样做:

代码语言:javascript
复制
public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new
            {
                // Add all routes we need MVC to handle here
                serverRoute = new ServerRouteConstraint(url =>
                {
                    return url.PathAndQuery.StartsWith("/qbo",
                        StringComparison.InvariantCultureIgnoreCase);
                })
            });

        // This is a catch-all for when no other routes matched. Let the Angular 2+ router take care of it
        routes.MapRoute(
            name: "angular",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index" } // The view that bootstraps Angular 2+
        );
    }

下面是路由约束类:

代码语言:javascript
复制
using System;
using System.Web;
using System.Web.Routing;

namespace Web
{
public class ServerRouteConstraint : IRouteConstraint
{
    private readonly Func<Uri, bool> _predicate;

    public ServerRouteConstraint(Func<Uri, bool> predicate)
    {
        this._predicate = predicate;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
        RouteValueDictionary values, RouteDirection routeDirection)
    {
        return this._predicate(httpContext.Request.Url);
    }
}
}

我已经用了很长一段时间了,不知道我可能受到了哪个博客的启发。

票数 0
EN

Stack Overflow用户

发布于 2018-09-13 16:56:03

我想出来了,不知道这是不是最好的方法,但它有效。

我创建了一个名为FEController (FrontEndController)的控制器。

代码语言:javascript
复制
public ActionResult Index()
{
  return File(Server.MapPath("/app/") + "index.html", "text/html");
}

然后在RouteConfig.cs中添加了一个地图路径。

代码语言:javascript
复制
routes.MapRoute(
   "Angular",
   "{app}/{*pathInfo}",
   new { controller = "FE", action = "Index", id = UrlParameter.Optional }
);

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

测试并确认工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52315613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档