CreatedAtRoute生成异常:“没有路由与提供的值匹配。”

问题描述 投票:0回答:1

使用我的Post函数在表中创建行会生成500响应并写入数据。响应主体是html,错误似乎如下:

<h1>An unhandled exception occurred while processing the request.</h1>
            <div class="titleerror">InvalidOperationException: No route matches the supplied values.</div>
                <p class="location">Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)</p>

显然,我的错误发生在我的CreatedAtRoute响应中。

我很难理解确切的帖子内容以及如何为表中的新条目生成链接。

我正在使用招摇呼叫端点。

我尝试阅读有关如何生成链接的内容,但是这些概念淹没了我。我尝试弄乱CreatedAtRoute响应,但似乎无法弄清楚需要发送的适当参数,或者它究竟能做什么。

控制器:

[ProducesResponseType(typeof(Location),200)]
        [HttpGet("locations/{locationId}", Name = "LocationById")]
        public IActionResult GetLocation(int locationId)
[HttpPost("locations")]
        public IActionResult CreateLocation([FromBody] CreateLocationRequest createLocationRequest)
        {
            try
            {
                if (createLocationRequest == null)
                {
                    return NotFound();
                }

                return CreatedAtRoute("LocationById",_repository.Location.CreateLocation(createLocationRequest),createLocationRequest);
            }
            catch (Exception e)
            {
                return StatusCode(500, e.InnerException);
            }
        }

存储库:

public int CreateCompany(string name, int countryId, int? parentId)//string name, int countryId, int? parentId
        {
            Company company = new Company()
            {
                CountryId = countryId,
                Name = name,
                ParentId = parentId
            };
            _context.Companies.Add(company);
            _context.SaveChanges();
            return company.Id;
        }

启动路由配置:

app.UseHttpsRedirection();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

我需要一条通往我新创建的表条目的正确路径。有关如何获得此建议的任何建议将不胜感激。

[了解了有关CreatedAtRoute的知识之后,我尝试调整函数以尝试查看在调用CreatedAtRoute时传递实体本身而不是模型是否会对我的事业有所帮助。它没有。

新方法:仓库:

public LocationReturns CreateLocation(CreateLocationRequest createLocationRequest)
        {
            if (_context.Companies.FirstOrDefault(c => c.Id == createLocationRequest.CompanyId) != null)
            {
                LocationReturns locationReturns=new LocationReturns();
                Location location = new Location()
                {
                    Address = createLocationRequest.Address,
                    AllowedAsSource = createLocationRequest.AllowedAsSource,
                    City = createLocationRequest.City,
                    CompanyId = createLocationRequest.CompanyId,
                    Name = createLocationRequest.Name,
                    Picture = createLocationRequest.Picture,
                    PostCode = createLocationRequest.Postcode,
                    Region = createLocationRequest.Region
                };
                _context.Locations.Add(location);
                _context.SaveChanges();
                locationReturns.id = location.Id;
                locationReturns.loc = location;
                return locationReturns;
            }
            LocationReturns loc = new LocationReturns();
            return loc;
        }

控制器:

[HttpPost("locations")]
        public IActionResult CreateLocation([FromBody] CreateLocationRequest createLocationRequest)
        {
            try
            {
                if (createLocationRequest == null)
                {
                    return NotFound();
                }
                LocationRepository.LocationReturns locationReturns=new LocationRepository.LocationReturns();
                locationReturns = _repository.Location.CreateLocation(createLocationRequest);

                return CreatedAtRoute("DefaultApi",new {id=locationReturns.id}, locationReturns.loc);
            }
            catch (Exception e)
            {
                return StatusCode(500, e.InnerException);
            }
        }

Where LocationReturns是一个结构。

c# asp.net-web-api ef-core-2.2
1个回答
0
投票

CreateAtRoute()内部生成的匿名对象必须具有一个与该路线中声明的参数同名的int属性。例如:[HttpGet("/locations/{locationId}, Name="LocationCreated"要求CreatedAtRoute("LocationCreated", new {locationId=id}, locationObject);而不是CreatedAtRoute("LocationCreated", new {id=_id}, locationObject);

© www.soinside.com 2019 - 2024. All rights reserved.