![](/img/trans.png)
[英]How to render Razor in cshtml page with Servicestack without content page
[英]How to render a razor page instead of static html content without redirecting
在我使用的旧 ASP.NET WebForms 中
HttpApplication.Context.RewritePath
Global.asax 中重写 url 的方法 - 重定向而不实际更改浏览器中显示的 URL。
现在我在 ASP.NET Core 3.0 中使用以下代码将一些静态内容重定向到剃刀页面:
public class RedirectHtmlRequests : IRule
{
private readonly string _docPath;
private readonly string _fldPublic;
private readonly string _fldPrivate;
public RedirectHtmlRequests(string docPath, string fldPublic, string fldPrivate)
{
this._docPath = docPath.ToLower();
this._fldPublic = fldPublic.ToLower();
this._fldPrivate = fldPrivate.ToLower();
}
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Path.Value.EndsWith(".htm", StringComparison.OrdinalIgnoreCase)
|| request.Path.Value.EndsWith(".html", StringComparison.OrdinalIgnoreCase))
{
//response.StatusCode = StatusCodes.Status301MovedPermanently;
//context.Result = RuleResult.EndResponse;
int id = FindDocumentByUri(request.Path);
if (id > 0)
{
//context.HttpContext.Response.Redirect(string.Format("/Dokument/{0}", id));
var response = context.HttpContext.Response;
response.StatusCode = StatusCodes.Status307TemporaryRedirect;
context.Result = RuleResult.EndResponse;
response.Headers[HeaderNames.Location] = (string.Format("/Dokument/{0}", id));
}
}
}
启动文件
app.UseRewriter(new RewriteOptions().Add(new RedirectHtmlRequests("Dokumenty/", "Verejne/", "Duverne/")));
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetParent(env?.ContentRootPath).FullName, "Dokumenty")),
RequestPath = "/Dokumenty"
});
但是线
response.Headers[HeaderNames.Location] = (string.Format("/Dokument/{0}", id));
实际上将浏览器窗口中的 url 重定向到/Dokument/{ID}
,这是不可取的。 我应该使用什么命令?
用指向剃刀页面的路由替换HttpContext.Request.Path
对我来说很有效:
if (id > 0)
{
context.HttpContext.Request.Path = (string.Format("/Dokument/{0}", id));
context.Result = RuleResult.SkipRemainingRules;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.