【问题标题】:MVC API Http RoutingMVC API Http 路由
【发布时间】:2019-06-24 10:15:46
【问题描述】:

我在 .NET Core 2.2 上有一个 MVC Web API

当我在本地运行我的 API 时,我的路由似乎都可以正常工作,但是当我将它部署到 Azure/我的服务器上时,路由似乎返回 404 错误。

这是我的“PricesController”,其中包含编码的路线:

namespace tf.PriceService.API.Controllers
{
[Route("HorseRacingApi/prices/")]
[Produces("application/json")]
[ApiController]
public class PricesController : Controller
{
    private readonly IPriceService _priceService;

    public PricesController(IPriceService priceService)
    {
        _priceService = priceService;
    }

    [HttpGet]
    [Route("GetPricesForRace/{meetingDate}/{courseId}/{raceNumber}/{ShowAll?}")]
    public IActionResult GetPricesForRace(DateTime meetingDate, int courseId, int raceNumber, bool? ShowAll = false)
    {
        return Ok(_priceService.GetPricesForRace(meetingDate, courseId, raceNumber));
    }

    [HttpGet]
    [Route("GetPriceForEntry/{meetingDate}/{courseId}/{raceNumber}/{horseCode}")]
    public IActionResult GetPriceForEntry(DateTime meetingDate, int courseId, int raceNumber, string horseCode)
    {
        return Ok(_priceService.GetPriceForEntry(meetingDate, courseId, raceNumber, horseCode));
    }

    [HttpGet]
    [Route("GetPriceForEntries/{ShowAll?}")]
    public IActionResult GetPriceForEntries(bool? ShowAll)
    {
        string jsonString = null;

        using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
        {
            jsonString = reader.ReadToEnd();
        }

        List<Core.Models.JsonEntryKey> list = JsonConvert.DeserializeObject<List<Core.Models.JsonEntryKey>>(jsonString);

        return Ok(_priceService.GetPriceForEntries(list));
    }
}

使用以下 URL 可在本地工作: https://localhost:44374/HorseRacingApi/prices/GetPricesForRace/2019-06-24/47/1

但不适用于 Azure 版本并返回 404。我错过了什么吗?此外,如果有帮助,返回类型是 JSON。

【问题讨论】:

    标签: azure .net-core model-view-controller routes


    【解决方案1】:

    请将您的 web.config 设置如下:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Somerandomname.WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
    

    更多信息,您可以访问此link。希望对您有所帮助。

    【讨论】:

    • 我确定我已经尝试过了,但似乎没有用。我猜“somerandomname”将是 API 解决方案的名称?
    猜你喜欢
    • 2019-03-29
    • 2016-12-06
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多