Что мне хотелось бы, это то, что я делаю неправильно, и чтобы URL оставался дружественным URL.
Вы можете напрямую изменить путь к redirectUrl в вашем методе ApplyRule. установите некоторые значения для _friendlyUrlMap
, чтобы облегчить тестирование, поскольку я не знаю определения _friendlyUrlMap
:
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
var path = request.Path.ToString().ToLower();
var _friendlyUrlMap = new Dictionary<string, string>();
_friendlyUrlMap.Add("/filetotexttohtml", "/Home/FileToTextToHtml");
_friendlyUrlMap.Add("/convertfile", "/Home/FileToTextToHtml");
if (_friendlyUrlMap.ContainsKey(path))
{
var redirectUrl = _friendlyUrlMap[path];
var response = context.HttpContext.Response;
context.HttpContext.Request.Path = redirectUrl;
}
}
. Вы можете добавить метод ApplyRule
в Startup.cs, а затем добавить его вConfigure
метод, подобный приведенному ниже:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRewriter(new RewriteOptions()
.Add(ApplyRule)
);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Результат: