Asp.net web api 为什么找不到我的控制器操作?
我的Web API项目中有一个控制器,其开头如下:Asp.net web api 为什么找不到我的控制器操作?,asp.net-web-api,routing,asp.net-mvc-routing,url-routing,Asp.net Web Api,Routing,Asp.net Mvc Routing,Url Routing,我的Web API项目中有一个控制器,其开头如下: [RoutePrefix("api/pricecompliance")] public class PriceComplianceController : ApiController …并具有此路由的Get方法: [Route("api/pricecompliance/{unit}/{year}/{month}")] public HttpResponseMessage Get(string unit, string year, strin
[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”路由前缀):
…我得到,“没有找到与请求URI匹配的HTTP资源
''
为什么找不到这种方法?我有其他遵循相同模式的控制器,它们相应的GET方法也很好
更新
在方法中预先添加可能多余的“[HttpGet]”也无助于解决问题
更新2
我仍然看不出代码有什么不同,一个工作,另一个不工作。以下是相关控制器代码:
价格合规控制器(不工作):
ProduceUsage控制器(正在工作):
以下是如何调用提供HTML的方法:
价格合规性(不起作用):
产品用途(正在运行):
以下是这些方法:
价格合规性(不起作用):
内部静态字符串转换器PriceComplianceFileBaseNameListToHtmlPartial(列表\u FileBaseNameList,字符串单位)
{
弦年;
弦短月;
StringBuilder=新的StringBuilder();
生成器。追加(“”);
建造商。附加(“价格合规”);
生成器。追加(“”);
builder.Append(“”);
//为每个报告创建链接
foreach(FileBaseNameList中的字符串fileBaseName)
{
年份=GetElement(3,fileBaseName);
shortmonth=GetElement(4,fileBaseName);
字符串filebasenamepritified=PrettifyPriceComplianceFBN(fileBaseName);
builder.Append(“”);
Append(string.Format(“,unit,shortmonth,year,filebasenameprified));
//http://localhost:52194/api/pricecompliance/GRAMPS/2016/Mar
builder.Append(“”);
}
返回builder.ToString();
}
生产用途(正在工作):
内部静态字符串转换器ProduceUsageFileBaseNameListToHtmlPartial(列表\u FileBaseNameList,字符串单位)
{
弦年;
弦短月;
StringBuilder=新的StringBuilder();
生成器。追加(“”);
建造商。附加(“生产用途”);
生成器。追加(“”);
builder.Append(“”);
//为每个报告创建链接
foreach(FileBaseNameList中的字符串fileBaseName)
{
shortmonth=GetElement(3,fileBaseName);
年份=GetElement(4,fileBaseName);
字符串fileBaseNamePrettified=PrettifyProduceUsageFBN(fileBaseName);
builder.Append(“”);
Append(string.Format(“,unit,shortmonth,year,filebasenameprified));
builder.Append(“”);
}
返回builder.ToString();
}
我不是在摸索如何找到一个,而另一个不是克隆的。您的路由参数称为
month
,但您的方法参数称为shortmonth
:
这可能是404错误背后的原因,模型绑定无法绑定shortmonth
参数(这不是可选的),因此会出现问题
[Route("{unit}/{year}/{month}")]
[RoutePrefix("api/pricecompliance")]
public class PriceComplianceController : ApiController
. . .
[Route("{unit}/{year}/{month}")]
[HttpGet]
public HttpResponseMessage Get(string unit, string year, string shortmonth)
{
byte[] excelContents;
. . .
[RoutePrefix("api/produceusage")]
public class ProduceUsageController : ApiController
. . .
[Route("{unit}/{shortmonth}/{year}")]
public HttpResponseMessage Get(string unit, string shortmonth, string year)
{
byte[] excelContents;
. . .
var htmlStr = ConvertPriceComplianceFileBaseNameListToHtmlPartial(_FileBaseNameList, _unit);
return htmlStr;
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();
}
[Route("{unit}/{year}/{month}")]
[HttpGet]
public HttpResponseMessage Get(string unit, string year, string shortmonth)