有没有办法让catch所有路由都提供静态文件?
看看这个http://blog.nbellocam.me/2016/03/21/routing-angular-2-asp-net-core/
我基本上想要这样的东西:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller}/{action=Index}");
routes.MapRoute("spa", "{*url}"); // This should serve SPA index.html
});
Run Code Online (Sandbox Code Playgroud)
因此,任何与MVC控制器不匹配的路由都将起作用 wwwroot/index.html
我有一个Asp.Net core 2.2项目。
最近,我将版本从.net core 2.2更改为.net core 3.0 Preview8。更改之后,我看到以下警告消息:
使用端点路由时,不支持使用“ UseMvc”配置MVC。要继续使用“ UseMvc”,请在“ ConfigureServices”中设置“ MvcOptions.EnableEndpointRouting = false”。
我了解通过将其设置EnableEndpointRouting为false可以解决此问题,但是我需要知道什么是解决问题的正确方法,以及为什么端点路由不需要UseMvc()功能。
我正在使用WebpackDevMiddleware for Development构建来提供使用客户端路由的Vue.js应用程序。可以从根URL很好地提供SPA应用程序,但是如果我尝试使用任何客户端深层链接,则会得到404。
请注意,按预期方式按生产运行。
我想要的是:
http://locahost/ -提供vue应用。http://localhost/overlays/chat -提供vue应用。http://localhost/api/* -提供服务器端处理的api路由。在该存储库中,该问题的最低限度可行复制。您可以在发生错误的开发环境中使用vscode调试来运行它。还有一个脚本/scripts/local-production将在生产环境中构建并运行,并在此环境中按预期工作。
我的Startup.cs的相关部分如下所示:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// In production, the Vue files will be served
// from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = Configuration["Client"];
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//set up default mvc routing
app.UseMvc(routes =>
{
routes.MapRoute("default", "api/{controller=Home}/{action=Index}/{id?}");
}); …Run Code Online (Sandbox Code Playgroud) 使用ASP.NET Core 1.0的MVC 6的RC1,您可以在调用时映射函数内的路径.我已经映射了一个"spa-fallback"路由,它将确保和视图是默认值,如下所示:Startup.Configureapp.UseMvcHomeControllerIndex
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
// ... omitted for brevity
app.UseExceptionHandler("/Home/Error");
app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute("spa-fallback", "{*anything}", new { controller = "Home", action = "Index" });
routes.MapWebApiRoute("defaultApi", "api/{controller}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
我希望回退,以便我的Angular2应用程序的路由不会导致HTTP状态代码404,Not Found.但是,当用户无意中尝试导航到不存在的页面视图时,我还需要正确处理.你可能会注意到我也打过电话app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");.
使用状态代码和"spa-fallback"路由重定向到我的错误页面的调用似乎是互斥的 - 这意味着我似乎只能拥有一个或另一个(但遗憾的是不是两者).有谁知道如何才能让两全其美?
角度Ver:5
我正在使用ASP.NET MVC进行新项目,并已与它集成了angular 5。该项目工作正常,除了页面刷新会产生404页面未找到错误。每当我从浏览器中刷新或重新加载页面时,就会出现404错误。
我已经完成了有关此Angular页面刷新问题的许多教程和讨论,但到目前为止还算不上成功。
我正在使用MVC共享视图_Layout.cshtml来指定角度lib引用和基本引用,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<base href="/">
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/bootstrap")
<script src="~/node_modules/core-js/client/shim.min.js"></script>
<script src="~/node_modules/zone.js/dist/zone.js"></script>
<script src="~/node_modules/systemjs/dist/system.src.js"></script>
<script src="~/Web/systemjs.config.js"></script>
<script>
System.import('Web/main.js').catch(function (err) { console.error(err); });
</script>
</head>
<body>
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
请建议我应该在哪里进行更改。