Reputation: 475
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
Upvotes: 2
Views: 3754
Reputation: 29986
URL rewriting can reduce the performance of an app. Where feasible, you should limit the number and complexity of rules.
If you want to route to Excel Action
in Excel Controller
with URL ExcelOnly
, you could try Route
like below:
public class ExcelController : Controller
{
[Route("ExcelOnly")]
public IActionResult Excel()
{
return View();
}
}
It will generate <li><a asp-action="Excel" asp-controller="Excel">Excel</a></li>
to
<a href="/ExcelOnly">Excel</a>
And when you enter http://localhost:64419/ExcelOnly
, it will route to Excel
action in Excel
controller.
Upvotes: 2
Reputation: 21
Herewith i mention solution for first problem. Please put below code in Layout page
<li> <a asp-area="" asp-controller="Excel" asp-action="Excel">Excel</a></li>
put below code in startup.cs page
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Upvotes: 0