FromUri in ASP.NET CORE 2.0

asp.net Coreasp.net Core-2.0

asp.net Core Problem Overview


This question is because in my MVC project with ASP.NET CORE 2.0, I can not add the [FromUri] attribute to my drivers.

I tried using libraries like System.Web.Http, but I did not recognize them. Then install these from Nuget Microsoft.AspNet.WebApi.Core "and" System.AppDomain.NetCoreApp, which resulted in being able to use System.Web.Http but due to ambiguity problems I was not allowed to use it, but if I found that [FromUri] existed.

The main question is whether in ASP.NET core 2.0 MVC or WebApi exists [FromUri] ?

if it exists, how to use it?

asp.net Core Solutions


Solution 1 - asp.net Core

I think you might be looking for [FromQuery]: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding#customize-model-binding-behavior-with-attributes

[FromUri] is used in Asp.Net WebApi 2, not asp.net core

Solution 2 - asp.net Core

TLDR : You can use [FromQuery] and [FromRoute] in place of [FromUri]


As Mike_G stated in his answer you can use [FromQuery] attribute in place of [FromUri]. But you might also need to use [FromRoute] in certain cases as pointed by Muhammad Umar in comments.

FromRoute

This attribute is used when the parameter is passed as the part of the URL string.

Ex: api/countries/1

FromQuery

As the name implies, FromQuery parameters has to be passed as query strings.

Ex: api/countries?id=1

References:

  1. FromRoute - Microsoft Docs
  2. FromQuery - Microsoft Docs
  3. FromUri - Microsoft Docs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexis ZapataView Question on Stackoverflow
Solution 1 - asp.net CoreMike_GView Answer on Stackoverflow
Solution 2 - asp.net CoreKolappan NView Answer on Stackoverflow