Fromuri in asp net core 2 0

Introduction

ASP.NET is a popular programming language used for building web . One of the key features of ASP.NET is the ability to input from various , including query strings, form data, and route . In this article, we will focus on the FromUri attribute in ASP.NET Core 2.0, which allows us to bind input parameters from the query string.

FromUri Attribute

The FromUri attribute is used to bind input parameters from the query string in ASP.NET Core 2.0. It is typically used in conjunction with the HttpGet attribute to handle GET . By default, ASP.NET Core binds parameters from the query string using the model feature. However, in some cases, we may want to specify that a be bound from the query string using the FromUri attribute.

Example

Let's consider an example where we have a UserController with a Get method that accepts a user ID as a query string parameter:

[HttpGet]
public IActionResult Get([FromUri] int userId)
{
    // Code to retrieve user details based on the user ID
    return Ok(userDetails);
}

In the above example, the FromUri attribute is applied to the userId parameter. This tells ASP.NET Core to bind the userId parameter from the query string. So, if we make a GET request to the UserController with a query string parameter “userId=123”, ASP.NET Core will automatically bind the value 123 to the userId parameter.

Conclusion

The FromUri attribute in ASP.NET Core 2.0 provides a convenient way to bind input parameters from the query string. By using this attribute, we can explicitly specify that a parameter should be bound from the query string, of relying on the default model binding behavior. This can be useful in scenarios where we want fine-grained control over how input parameters are bound in our ASP.NET Core applications.

Rate this post

Leave a Reply

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

Table of Contents