Friendly urls in asp net mvc 6 asp net core or asp net5

Introduction

ASP.NET is a popular language used for building web applications. One of the key of ASP.NET is its ability to create friendly URLs, which are user-friendly and easy to remember. In this article, we will explore how to implement friendly URLs in ASP.NET MVC 6, ASP.NET Core, or ASP.NET 5.

What are Friendly URLs?

Friendly URLs are URLs that are easy to read and understand for both users and search engines. They typically contain descriptive words or phrases that accurately represent the content of the web page. For example, instead of having a URL like “www.example.com/product?id=123”, a friendly URL would look like “www.example.com/product/123”.

Implementing Friendly URLs in ASP.NET

To implement friendly URLs in ASP.NET, we can make use of routing. Routing is the process of mapping URLs to specific actions or controllers in our application. By routes, we can create friendly URLs that match our desired structure.

Defining Custom Routes

In ASP.NET, we can define custom routes in the Startup.cs file. Let's take a look at an example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "product",
            template: "product/{id}",
            defaults: new { controller = "Product", action = "" }
        );
    });
}

In the above example, we define a custom route “product” that maps to the “Details” action of the “Product” controller. The route template specifies that the URL start with “product/” followed by the product ID. For example, “www.example.com/product/123” will be mapped to the “Details” action with the product ID as a parameter.

Using Friendly URLs in Views

Once we have defined our custom routes, we can use them in our views to friendly URLs. We can make use of the Url.Action method to generate the URL based on the route name. Here's an example:

In the above example, we use the Url.Action method to generate a friendly URL for the “Details” action of the “Product” controller with the product ID as a parameter. The resulting URL will be something like “www.example.com/product/123”.

Conclusion

Friendly URLs are an important aspect of web as they improve the user experience and make it easier for search engines to understand the content of our web pages. In this article, we explored how to implement friendly URLs in ASP.NET MVC 6, ASP.NET Core, or ASP.NET 5 by defining custom routes and using the Url.Action helper method. By following these techniques, we can create user-friendly and SEO-friendly URLs for our ASP.NET applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents