[英]need route for my web api 2 controller
我有一個簡單的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方法。 而是使用以下URL瀏覽
CBMI.WebAPIservice/api/markers/GetGrantsIS
路由到Get方法,該方法識別id沒有值。 然后它默認值為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。
但是,您可以使用屬性路由獲得所需的結果。
如果要使用屬性路由,則已在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")
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.