【发布时间】:2015-09-19 23:24:42
【问题描述】:
我正在将我的项目升级到 ASPNET5。我的应用程序是一个使用 HTML5 Url Routing (HTML5 History API) 的 AngularJS Web 应用程序。
在我之前的应用程序中,我使用了 URL 重写 IIS 模块,代码如下:
<system.webServer>
<rewrite>
<rules>
<rule name="MainRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
<system.webServer>
我意识到我可以移植它,但我想最小化我的 Windows 依赖项。根据我的阅读,我认为我应该能够使用 ASP.NET 5 中间件来完成此操作。
我认为代码看起来像这样,但我认为我还差得很远。
app.UseFileServer(new FileServerOptions
{
EnableDefaultFiles = true,
EnableDirectoryBrowsing = true
});
app.Use(async (context, next) =>
{
if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("api"))
{
await next();
}
else
{
var redirect = "http://" + context.Request.Host.Value;// + context.Request.Path.Value;
context.Response.Redirect(redirect);
}
});
基本上,我想要路由包含/api 或/signalr 的任何内容。关于在 ASPNET5 中完成此任务的最佳方法有什么建议吗?
【问题讨论】:
-
是的,您可以通过
middleware实现这一目标 -
想展示你如何做到这一点?
-
GitHub上有很多middleware样本。我写的一个:github.com/aguacongas/chatle/tree/dev/src/ChatLe.HttpUtility
标签: asp.net .net html asp.net-core asp.net-web-api-routing