Url rewriting ASP.NET核心RC2中的Url重写
如何在ASP.NET Core RC2中完成URL重写?当我刷新页面时,从RC1迁移到RC2破坏了我的Angular 2路由 我以前在wwwroot中的web.config中使用过类似的规则。对于RC2,我甚至不确定我的wwwroot中是否应该有一个web.config,或者我是否应该在我的项目基础中有一个web.config 这是我的基本web.configUrl rewriting ASP.NET核心RC2中的Url重写,url-rewriting,asp.net-core,Url Rewriting,Asp.net Core,如何在ASP.NET Core RC2中完成URL重写?当我刷新页面时,从RC1迁移到RC2破坏了我的Angular 2路由 我以前在wwwroot中的web.config中使用过类似的规则。对于RC2,我甚至不确定我的wwwroot中是否应该有一个web.config,或者我是否应该在我的项目基础中有一个web.config 这是我的基本web.config <?xml version="1.0" encoding="utf-8"?> <configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="true" />
</system.webServer>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!--Redirect selected traffic to index -->
<rule name="Index Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/account/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
这是我的wwwroot web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="true" />
</system.webServer>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!--Redirect selected traffic to index -->
<rule name="Index Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/account/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
当我刷新angular 2路由时,我得到一个
状态代码:404;在ASP.NET中找不到
我遇到了完全相同的问题。事实上,对我来说,当重写规则包含在web.config文件中时,IIS Express服务器甚至没有启动。考虑到这一点,我四处寻找其他方法来做同样的事情,而不依赖重写规则。我发现可以使用startup.cs文件中的MapWhen函数将MVC未处理的任何内容发送回index.html
在app.UseMvc()调用之后,以下代码已添加到Startup.cs类的Configure方法中
到目前为止,这看起来像它的工作,但我需要做一些更多的测试,看看是否有任何副作用。就个人而言,重写规则似乎是一个更好的解决方案,但这似乎让我克服了这个问题。我找到了Steve Sanderson的解决方案,似乎有效。 他写了RouteBuilder的扩展。 您可以通过调用扩展方法MapSpaFallbackRoute在Setup.cs中对其进行配置
我找到了一个有效的解决办法 下面是我的Configure方法中为我解决问题的代码。 但要小心,申报顺序很重要
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) => {
await next();
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) {
context.Request.Path = "/";
context.Response.StatusCode = 200;
await next();
}
});
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(routes => {
routes.MapRoute("Default", "api/{controller}/{action}/{id?}");
});
}
这个句柄是否允许其他mime类型或API调用通过?我们在同一台服务器上为locale和api提供了json……使用上下文的mapwhen调用的第一部分是决定哪些调用转到MVC,哪些调用转到静态文件处理程序。因此,您可以显式地将您的API调用路由到服务器,并将其他所有内容发送回angular(或其他)应用程序。在我的具体例子中,我们只向MVC发送包含“/api/”的任何路径。发现此中间件代码很有用。我们正在Asp.net core 1 RC1项目中使用类似的东西。需要将其移植到RC2/RTM。。。这就是我刚才谈到的解决方案,它非常棒。你要做的就是安装软件包。。。在我的例子中,我只需要添加MapSpaFallbackRoute,在我的家庭索引控制器中,我所做的就是返回文件(“Index.html”、“text/html”),一切都运行得非常出色。谢谢您必须添加一个控制器才能工作吗?我来自java/node世界,不熟悉MS世界,但我会尽可能保持我的项目干净。i、 这是我用来解决同样问题的答案。它有一个在公认的答案中找不到的好处,即满足OP希望使用最少数量的.NET代码的愿望。