3

我有一个简单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.

+0

您应该使用RouteAttribute包括在同一控制器的多条途径获取。 https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 Theres不需要改变地图路线。 – Fals

回答

3

Web Api 2支持REST架构,这意味着它期望您的操作为GET,POST,PUT,DELETE。

但是,您可以使用attribute routing来获得所需的结果。

如果你想使用属性路由,你已经在你的WebApiConfig文件中有该设置。所以,你只需要修改代码以使用路由属性,像这样:

[Route("api/markers/getgrantsis")] 
public HttpResponseMessage GetGrantsIS() 
{ 
    XmlDocument docContent = GetXmlDataFromDB(); 
    return new HttpResponseMessage 
    { 
    Content = new StringContent(docContent.InnerXml.ToString(), Encoding.UTF8, "application/xml") 
    }; 

} 
+0

谢谢;这现在起作用。关于你提出的REST评论,我想如果我的方法以Get ..开头(如上面的GetGrantsIS),那么它可以在不使用属性路由的情况下解决。我想到了与一个新的控制器完全接近。 –

+0

这是真的,但它不会更改URL。例如,您可以使用GetGrantsIS()并通过/ api/markers /访问它,但它不能通过/ api/markers/GetGrantsIs访问。在第二种情况下,框架将尝试将GetGransIS解析为Id字段的int,或者完全忽略它。如果你想要通过URL访问自定义get方法,最好的选择就是属性路由。希望这是有道理的。 – Woot

相关问题