【发布时间】:2016-04-04 21:16:01
【问题描述】:
我的 Web API 项目中有一个控制器,开头如下:
[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController
...并且有一个带有此路由的 Get 方法:
[Route("api/pricecompliance/{unit}/{year}/{month}")]
public HttpResponseMessage Get(string unit, string year, string shortmonth)
但是,尝试导航到此 URL:
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 的方法的调用方式:
价格合规(无效):
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();
}
我不知道如何找到一个,而另一个却没有,因为它们是如此克隆。
【问题讨论】:
标签: asp.net-web-api routing asp.net-mvc-routing url-routing