我有一個簡單WebApi2控制器返回XML,但我不能用我定義了路由適當加另一種方法:需要的路線爲我的網頁API 2控制器
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}"
);
}
}
}
I C注意瞭解如何更改路由以識別動作,以便調用方法。而是使用以下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.
您應該使用RouteAttribute包括在同一控制器的多條途徑獲取。 https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 Theres不需要改變地圖路線。 – Fals