locked
Web Api CRUD - internal server error / how to fix? RRS feed

  • Question

  • User379720387 posted

    This code all works as new usernames are added to the table. However, response._StatusCode says internal server error.

    There is something going on with ActionContext and I have found some threads explaining the issue. Here is one

    My knowledge is insufficient to determine how the suggested fixes are applicable what I am doing.

    Please advise.

    newUserName.created = DateTime.UtcNow;
            newUserName.modified = DateTime.UtcNow;
            var httpClient = ClientFactory.CreateClient("ServerAPI");
            var response = await httpClient.PostAsJsonAsync("api/usernames", newUserName);

    the controller:

    [HttpPost]
            public async Task<ActionResult<UserName>> PostUserName(UserName userName)
            {
                _context.Add(userName);
                await _context.SaveChangesAsync();
    
                return new CreatedAtRouteResult("GetUserName", new { id = userName.NameId }, userName);
            }
    An unhandled exception occurred while processing the request.
    InvalidOperationException: No route matches the supplied values.
    Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)
    
    Stack Query Cookies Headers
    InvalidOperationException: No route matches the supplied values.
    Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)
    Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
    Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync<TFilter, TFilterAsync>()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
    Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
    Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
    Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
    Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)
    Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

    Tuesday, June 23, 2020 4:43 PM

Answers

All replies

  • User475983607 posted

    Does the named route "GetUserName" exist?   

    return new CreatedAtRouteResult("GetUserName", new { id = userName.NameId }, userName);

    Tuesday, June 23, 2020 4:54 PM
  • User379720387 posted

    In the same controller:

    [HttpGet("{id}")]
            public async Task<ActionResult<UserName>> GetUserName(int id)
            {
                var userName = await _context.UserNames.FindAsync(id);
    
                if (userName == null)
                {
                    return NotFound();
                }
    
                return userName;
            }

    Tuesday, June 23, 2020 5:43 PM
  • User475983607 posted

    That's not a named route; https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#route-name.

    [HttpGet("{id}" Name = "GetUserName")]
    public async Task<ActionResult<UserName>> GetUserName(int id)



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 5:47 PM
  • User379720387 posted

    Yup!

    did this one as well:

    [HttpDelete("{id}", Name = "DeleteUserName")]

    thnx

    Tuesday, June 23, 2020 6:38 PM