I'm a desktop developer. But i need to learn how to do web development using ASP.net Core. So i am here now asking question. How do i properly rewrite the URL of my website:
This is the structure of my website
This is how I Add link to my layout page
<li><a asp-action="Index" asp-controller="Home">Home</a></li>
<li><a asp-action="Excel" asp-controller="Excel">Excel</a></li>
What is want to do is rewrite this URL
http://localhost:64419/Excel/Excel
Currently I am using this middleware
https://www.nuget.org/packages/Microsoft.AspNetCore.Rewrite/
And this is my code
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseMvc(routes =>
{
var rewrite = new RewriteOptions()
.AddRewrite(@"ExcelOnly", "Excel/Excel", skipRemainingRules: false);
app.UseRewriter(rewrite);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}"
);
routes.MapRoute(
name: "excel",
template: "{controller=Excel}/{action=Excel}"
);
});
}
Because of the code above I was able to rewrite the URL into
http://localhost:64419/ExcelOnly
By simply typing the Link in the URL.
But when I clicked on the Link in the Layout page. The page is loaded but it uses the link http://localhost:64419/Excel/Excel how can I properly rewrite the URL.
What are the things I need to search. I can't find a solution. Maybe I'm using the wrong search term. So I decided to ask here.
Thank you.
Update:
I was able to redirect the page in the URL i want
with this code
var rewrite = new RewriteOptions()
.AddRedirect("Excel/Excel", "ExcelOnly")
.AddRewrite(@"ExcelOnly", "Excel/Excel", skipRemainingRules: false);
app.UseRewriter(rewrite);
But is it a good practice? I always need the Controller and the Action in each of my redirect. So if i changed the Action name i also need to change the redirect and rewrite.
And another problem. When I hover the mouse in the link it shows the path http://localhost:64419/Excel/Excel so how can I hide this stuff?
Thank you
