error when I tried to findBy string in API

ahmed omar 181 Reputation points
2023-02-02T20:42:16.4033333+00:00

Hello,

currently, I'm learning Swagger, when I tried to create find by username ,I got the below error


        [HttpGet]
        [Route("{fullname:string}")]

        public async Task<IActionResult> signIn([FromRoute] string fullname)
        {
            Console.WriteLine(fullname);
            var woner = await dbContext.PetWoner.FindAsync(fullname);
            if (woner == null)
            {
                return NotFound();
            }
            return Ok(woner);
        }

InvalidOperationException: The constraint reference 'string' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'.

  • Microsoft.AspNetCore.Routing.DefaultParameterPolicyFactory.Create(RoutePatternParameterPart parameter, string inlineText)
  • Microsoft.AspNetCore.Routing.ParameterPolicyFactory.Create(RoutePatternParameterPart parameter, RoutePatternParameterPolicyReference reference)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder+DfaBuilderWorker.AddParentsWithMatchingLiteralConstraints(List<DfaNode> nextParents, DfaNode parent, RoutePatternParameterPart parameterPart, IReadOnlyList<RoutePatternParameterPolicyReference> parameterPolicyReferences)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder+DfaBuilderWorker.ProcessSegment(RouteEndpoint endpoint, List<DfaNode> parents, List<DfaNode> nextParents, RoutePatternPathSegment segment)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder+DfaBuilderWorker.ProcessLevel(int depth)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.BuildDfaTree(bool includeLabel)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder.Build()
  • Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.CreateMatcher(IReadOnlyList<Endpoint> endpoints)
  • Microsoft.AspNetCore.Routing.DataSourceDependentCache<T>.Initialize()
  • System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
  • System.Threading.LazyInitializer.EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
  • Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher..ctor(EndpointDataSource dataSource, Lifetime lifetime, Func<MatcherBuilder> matcherBuilderFactory)
  • Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource)
  • Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()
  • Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.<Invoke>g__AwaitMatcher|8_0(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask)
  • Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,576 Reputation points
    2023-02-02T20:49:48.9466667+00:00

    You don't need to specify the type in the route as the parameter is already defined as a string. Since everything is convertable to a string the constraint does nothing. You only need to add a constraint name if you are defining a custom constraint. Also you should use HttpGet with the route.

    [HttpGet("{fullname}")]
    public async Task<IActionResult> signIn( string fullname) {...}
    

    But I should point out that sign in should be a POST, not a GET. GETs can be cached and it is a security risk to expose a signin like this. The actual login name should be part of the body and not part of the URL since bodies are generally not captured in intermediate caches. GETs cannot have bodies.

    0 comments No comments

0 additional answers

Sort by: Most helpful