【问题标题】:Routing in ASP.net core with Angular使用 Angular 在 ASP.net 核心中进行路由
【发布时间】:2021-10-31 08:03:38
【问题描述】:

我在 VS 2019 中创建了一个带有 Angular 项目的 Asp.net 核心 Web Api。我接受了所有默认设置并成功启动。我的问题是如何选择 ClientApp/src 下的 index.html(以及 Angular 内容/组件)作为默认页面发送到 bowser。从 Startup.cs 的 Configure 方法中,我可以看到路由配置为:

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });

我错过了什么?我的目标是最终在一个项目中创建 MVC、Angular、Areas 和 Api 的组合。

更新: 下面是SPA部分

 app.UseSpa(spa =>
      {//comments omitted
           spa.Options.SourcePath = "ClientApp";

           if (env.IsDevelopment())
           {
               spa.Options.StartupTimeout = new TimeSpan(0, 0, 80);
               spa.UseAngularCliServer(npmScript: "start");
            }
       });

TIA

【问题讨论】:

  • 底部有 App.UseSpa(...) 部分吗?
  • @TodorPavlovic 是的。你知道这将如何决定幕后的路线吗?

标签: angular asp.net-core .net-core asp.net-core-webapi


【解决方案1】:

如果您的目标是访问特定的控制器操作方法,请尝试使用 app.MapControllers();

app.UseEndpoints(endpoints =>
 {
            endpoints.MapControllers();
 });

这将允许您使用属性路由调用端点。

【讨论】:

  • 可能会这样做。但仍不清楚当前路由是如何工作的。我没有发布完整的代码,因为不知道哪个部分与默认的 Web API Angular 项目相关。
【解决方案2】:

每个网络请求都进入 HTTP 请求管道。该管道是在Startup.Configure 方法中构建的。 网络请求从上到下遍历管道,网络响应遍历管道回到顶部。

最基本、最简单的中间件如下所示:

app.Use(async (context, next) => {
    // This middleware can manipulate the HTTP response headers,
    // response body, cookies, ...

    // We can decide if the next middleware should be called or not.
    // Sometimes this may not be necessary
    // (eg. serving a Sitemap, or a static file)
    await next();
});

在您的情况下,您有以下管道:

// This middleware reads the identity cookie and loads
// the information in the HttpContext.User. Finally calls the next middleware.
app.UseAuthentication();

// This middleware does something about routing decisions.
app.UseRouting();

// This middleware evaluates the AuthorizeAttribute to check if the route is accessible with the current Identity (which has been loaded before).
app.UseAuthorization();

// This middleware decides which controller method should be called. If a controller method matches the request url, the next middleware will not be called.
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller}/{action=Index}/{id?}"
    );
});

// If none of the above middleware has "handled" the request, or more precisely,
// If each of the above middleware has called the next() delegate,
// The webrequest will end up in this middleware.
// As far as I know, the UseSpa middleware will never call the next() middleware.
app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";
    if (env.IsDevelopment())
    {
        spa.Options.StartupTimeout = new TimeSpan(0, 0, 80);
        spa.UseAngularCliServer(npmScript: "start");
    }
});

【讨论】:

  • 太棒了。正是我正在寻找的。谢谢!
猜你喜欢
  • 2021-09-07
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2019-09-10
  • 1970-01-01
  • 2016-10-29
  • 2020-05-30
相关资源
最近更新 更多