在 ASP.net 核心路由 Angular
Routing in ASP.net core with Angular
我在 VS 2019 中使用 Angular 项目创建了一个 Asp.net 核心 Web Api。我接受了所有默认设置并成功启动。我的问题是如何选择 ClientApp/src 下的 index.html(连同 Angular content/components)作为默认页面发送到浏览器。从 Startup.cs 的 Configure 方法中,我可以看到路由配置为:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
我错过了什么?我的目标是最终在一个项目中创建 MVC、Angular 与区域和 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.MapControllers();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
这将允许您使用属性路由调用端点。
每一个webrequest都会进入HTTP请求管道。此管道是在 Startup.Configure
方法中构建的。
webrequest从上到下遍历管道,webresponse从上到下遍历管道。
最基本、最简单的中间件如下所示:
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");
}
});
我在 VS 2019 中使用 Angular 项目创建了一个 Asp.net 核心 Web Api。我接受了所有默认设置并成功启动。我的问题是如何选择 ClientApp/src 下的 index.html(连同 Angular content/components)作为默认页面发送到浏览器。从 Startup.cs 的 Configure 方法中,我可以看到路由配置为:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
我错过了什么?我的目标是最终在一个项目中创建 MVC、Angular 与区域和 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.MapControllers();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
这将允许您使用属性路由调用端点。
每一个webrequest都会进入HTTP请求管道。此管道是在 Startup.Configure
方法中构建的。
webrequest从上到下遍历管道,webresponse从上到下遍历管道。
最基本、最简单的中间件如下所示:
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");
}
});