C#-我的Web API 2控制器需要路由
作者:互联网
我有一个简单的WebApi2控制器,该控制器返回XML,但无法使用定义的路由正确添加另一个方法:
namespace CBMI.WebAPIservice.Controllers
{
public class MarkersController : ApiController
{
public HttpResponseMessage Get(int? id)
{
int i = id.HasValue ? id.Value : 0;
XmlDocument docContent = GetXmlDataFromDB(i);
return new HttpResponseMessage
{
Content = new StringContent(docContent.InnerXml.ToString(), Encoding.UTF8, "application/xml")
};
}
public HttpResponseMessage GetGrantsIS()
{
XmlDocument docContent = GetXmlDataFromDB();
return new HttpResponseMessage
{
Content = new StringContent(docContent.InnerXml.ToString(), Encoding.UTF8, "application/xml")
};
}
public XmlDocument GetXmlDataFromDB()
{
string connStr = System.Convert.ToString(
System.Web.Compilation.ConnectionStringsExpressionBuilder.GetConnectionString("MDWConnectionString"),
System.Globalization.CultureInfo.CurrentCulture);
SqlConnection conn = new SqlConnection(connStr);
SqlCommand sqlCmd = new SqlCommand("dbo.FLAS_List_GrantLocationsByAmount_V1", conn);
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
XmlDocument xmlDoc = new XmlDocument();
XmlReader xmlReader = sqlCmd.ExecuteXmlReader();
if (xmlReader.Read())
xmlDoc.Load(xmlReader);
conn.Close();
return xmlDoc;
}
public XmlDocument GetXmlDataFromDB(int worldAreaID )
{
string scrambleAward = "";
string connStr = System.Convert.ToString(
System.Web.Compilation.ConnectionStringsExpressionBuilder.GetConnectionString("MDWConnectionString"),
System.Globalization.CultureInfo.CurrentCulture);
SqlConnection conn = new SqlConnection(connStr);
SqlCommand sqlCmd = new SqlCommand("dbo.FLAS_List_Awards_V1", conn);
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@AreaID", worldAreaID);
sqlCmd.Parameters.AddWithValue("@Scramble", scrambleAward);
conn.Open();
XmlDocument xmlDoc = new XmlDocument();
XmlReader xmlReader = sqlCmd.ExecuteXmlReader();
if (xmlReader.Read())
xmlDoc.Load(xmlReader);
conn.Close();
return xmlDoc;
}
}
}
WebApiConfig.cs
namespace CBMI.WebAPIservice.App_Start
{
// This code file defines the delegate where you should put your Web API configuration code.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute
(
name: "WebApi2",
routeTemplate: "api/{controller}/{id}"
);
config.Routes.MapHttpRoute
(
name: "ApiGrantsIS",
routeTemplate: "api/{controller}/{action}"
);
}
}
}
我不明白如何更改路由以识别操作,从而调用GetGrantsIS方法.而是使用以下网址浏览
CBMI.WebAPIservice/api/markers/GetGrantsIS
路由到识别该id没有值的Get方法.然后,它的默认值为0,它可以工作,但是我需要让此URL调用GetGrantsIS方法.
编辑:尝试添加属性路由给出了新的错误
我装饰如下:
[Route("api/{controller}/GetGrantsIS")]
public HttpResponseMessage GetGrantsIS()
现在我得到了:
Server Error in '/CBMI.WebAPIservice' Application.
A direct route cannot use the parameter 'controller'. Specify a literal path in place of this parameter to create a route to a controller.
解决方法:
Web Api 2支持REST体系结构,这意味着它希望您的操作是GET,POST,PUT,DELETE.
但是,使用attribute routing可以获得理想的结果.
如果要使用属性路由,则已经在WebApiConfig文件中具有该设置.因此,您只需要修改代码即可使用Route属性,如下所示:
[Route("api/markers/getgrantsis")]
public HttpResponseMessage GetGrantsIS()
{
XmlDocument docContent = GetXmlDataFromDB();
return new HttpResponseMessage
{
Content = new StringContent(docContent.InnerXml.ToString(), Encoding.UTF8, "application/xml")
};
}
标签:asp-net-web-api2,asp-net-web-api-routing,routes,asp-net-apicontroller,c 来源: https://codeday.me/bug/20191111/2023188.html