为什么找不到我的Controller Action?

时间:2016-04-04 21:16:02

标签: asp.net-web-api routing asp.net-mvc-routing url-routing

我的Web API项目中有一个Controller,就像这样开始:

[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController

...并且有一个使用此路由的Get方法:

[Route("api/pricecompliance/{unit}/{year}/{month}")] 
public HttpResponseMessage Get(string unit, string year, string shortmonth)

但是,尝试导航到此网址:

http://localhost:52194/api/pricecompliance/GRAMPS/2016/Mar

...给了我Web API的404页面。

如果我减少了到此的路由(因为控制器已经假设假设" api / pricecompliance"路由前缀):

[Route("{unit}/{year}/{month}")]

..我明白了," 没有找到与请求URI匹配的HTTP资源 ' http://localhost:52194/api/pricecompliance/GRAMPS/2016/Mar' "

为什么找不到该方法?我有其他控制器遵循相同的模式,并找到相应的GET方法。

更新

预设可能是多余的" [HttpGet]"该方法也无济于事。

更新2

我仍然无法看到代码是如何不同的,因此一个有效,另一个无效。以下是相关的控制器代码:

PriceCompliance控制器(不工作):

[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController
. . .

[Route("{unit}/{year}/{month}")]
[HttpGet]
public HttpResponseMessage Get(string unit, string year, string shortmonth)
{
    byte[] excelContents;
    . . .

ProduceUsage控制器(正在运行):

[RoutePrefix("api/produceusage")]
public class ProduceUsageController : ApiController
. . .

    [Route("{unit}/{shortmonth}/{year}")]
    public HttpResponseMessage Get(string unit, string shortmonth, string year)
    {
        byte[] excelContents;
        . . .

以下是如何调用提供HTML的方法:

PriceCompliance(不工作):

var htmlStr = ConvertPriceComplianceFileBaseNameListToHtmlPartial(_FileBaseNameList, _unit);
return htmlStr;

ProduceUsage(正在运行):

var htmlStr = ConvertProduceUsageFileBaseNameListToHtmlPartial(_FileBaseNameList, _unit);
return htmlStr;

以下是这些方法:

价格合规(不合规):

internal static string ConvertPriceComplianceFileBaseNameListToHtmlPartial(List<string> _FileBaseNameList, string unit)
{
    string year;
    string shortmonth;

    StringBuilder builder = new StringBuilder();
    builder.Append("<h2>");
    builder.Append("Price Compliance");
    builder.Append("</h2>");
    builder.Append("<p></p>");

    // Create links for each report
    foreach (String fileBaseName in _FileBaseNameList)
    {
        year = GetElement(3, fileBaseName);
        shortmonth = GetElement(4, fileBaseName);
        string fileBaseNamePrettified = PrettifyPriceComplianceFBN(fileBaseName);
        builder.Append("<p></p>");
        builder.Append(string.Format("<a href=\"pricecompliance/{0}/{1}/{2}\">{3}</a>", unit, shortmonth, year, fileBaseNamePrettified)); 
        //http://localhost:52194/api/pricecompliance/GRAMPS/2016/Mar
        builder.Append("<p></p>");
    }
    return builder.ToString();
}

生产用法(正在运作):

internal static string ConvertProduceUsageFileBaseNameListToHtmlPartial(List<string> _FileBaseNameList, string unit)
{
    string year;
    string shortmonth;

    StringBuilder builder = new StringBuilder();
    builder.Append("<h2>");
    builder.Append("Produce Usage");
    builder.Append("</h2>");
    builder.Append("<p></p>");

    // Create links for each report
    foreach (String fileBaseName in _FileBaseNameList)
    {
        shortmonth = GetElement(3, fileBaseName);
        year = GetElement(4, fileBaseName);
        string fileBaseNamePrettified = PrettifyProduceUsageFBN(fileBaseName);
        builder.Append("<p></p>");
        builder.Append(string.Format("<a href=\"produceusage/{0}/{1}/{2}\">{3}</a>", unit, shortmonth, year, fileBaseNamePrettified));
        builder.Append("<p></p>");
    }

    return builder.ToString();
}

我并不知道如何找到一个,而另一个不是当他们如此克隆时。

1 个答案:

答案 0 :(得分:2)

您的路线参数名为month,但您的方法参数名为shortmonth

[Route("{unit}/{year}/{month}")]
[HttpGet]
public HttpResponseMessage Get(string unit, string year, string shortmonth)

这可能是您的404错误背后的原因,模型绑定无法绑定shortmonth参数(这不是可选的)因此会抱怨。