Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this routing in program class in ASP.NET Core 6:
app.MapControllerRoute(
    name: "default",
    pattern: "{action}/{id}/{Title?}"
);

In my controller I have this action :

public IActionResult Index(int id)
{
    string title = get-Item-Title-From-Database-By-Id(id);
    return View();
}


I want show title in URL, like code project
https://codeproject.com/questions/questionId/questionTitle


Is there a way to do this? Thanks.

What I have tried:

ASP.NET Routing
from microsoft
Posted
Updated 16-Jul-23 20:24pm
Comments
Andre Oosthuizen 16-Jul-23 14:23pm    
Why not like CodeProject? :) just kidding...

1 solution

What you mention seems to be URL Rewriting which is at application level (not a specific even inside the application). Handling only one request in other way is not URL rewriting implementation.

You can approach[^] it either using:
- ASP.NET Routing
Quote:
ASP.NET routing is used to dispatch a request to a handler based on the requested URL path. As opposed to URL rewriting, the routing module knows about the handlers and selects the handler that should generate a response for the requested URL. You can think of ASP.NET routing as an advanced handler-mapping mechanism.

- IIS URL Rewriting
Quote:
URL rewriting is used to manipulate URL paths before the request is handled by the Web server. The URL rewriting module does not know which handler will eventually process the rewritten URL. In addition, the actual request handler might not know that the URL has been rewritten.


For more details, read here:
Using the URL Rewrite Module | Microsoft Learn[^]
Quote:
The Microsoft URL Rewrite Module 2.0 for IIS 7 and above enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.

URL Rewriting Middleware in ASP.NET Core | Microsoft Learn[^]
Quote:
URL rewriting is the act of modifying request URLs based on one or more predefined rules. URL rewriting creates an abstraction between resource locations and their addresses so that the locations and addresses aren't tightly linked.


A sample here: An Example of URL Rewriting With ASP.Net[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900