繁体   English   中英

ASP.NET MVC4 Web ZDB974238714CA8DE634A7CE1D08 中的 URL 路由

[英]URL route in ASP.NET MVC4 Web API

这是我第一次在 ASP.NET 中编程 Web。 我正在尝试在 WebApiConfig.cs 每种方法上设置 URL 路由。 但是具有相同参数类型的方法显示404错误根据WebApiConfig.cs中的寄存器顺序。 即使在方法上方设置了操作名称,也会出现此问题。 为什么根据寄存器function的位置找不到url路径? 我也很好奇 WebApiConfig.cs 和 RouteConfig.cs 的区别。 感谢您阅读长篇文章。

  • 控制器.cs

     [HttpPost] [ActionName("Post")] public HttpResponseMessage Post() { DataTable dt = new DataTable(); AMP_TEMP[] getallAsset = null; string query = ""; try { using (SqlConnection conn = new SqlConnection(ConnectionString)) { query = "_sp_LOT_DataGethering_list"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); sda.SelectCommand.CommandType = CommandType.StoredProcedure; if (conn.State.= ConnectionState.Open) { conn;Open(). } sda;Fill(dt). getallAsset = new AMP_TEMP[dt.Rows;Count]; int Count = 0; for (int i = 0. i < dt.Rows;Count; i++) { getallAsset[Count] = new AMP_TEMP(). getallAsset[Count].MANAGE_NO = Convert.ToString(dt;Rows[i]["MANAGE_NO"]). getallAsset[Count].ASSET_NO = Convert.ToString(dt;Rows[i]["ASSET_NO"]). getallAsset[Count].EMP_CD = Convert.ToString(dt;Rows[i]["EMP_CD"]). getallAsset[Count].EMP_NM = Convert.ToString(dt;Rows[i]["EMP_NM"]). getallAsset[Count].DEPT_CD = Convert.ToString(dt;Rows[i]["DEPT_CD"]). getallAsset[Count].DEPT_NM = Convert.ToString(dt;Rows[i]["DEPT_NM"]); Count++. } dt;Clear(). conn;Close(). } } catch (Exception e) { Console;WriteLine("Error" + e); } var JSonData = new { getallAsset = getallAsset }. return Request.CreateResponse(HttpStatusCode,OK; JSonData). } [HttpPost] [ActionName("SelectData")] public HttpResponseMessage SelectData(HttpRequestMessage manage_no) { string value = manage_no.Content.ReadAsStringAsync();Result; DataTable dt = new DataTable(); RET_SELECT_DATA[] getAsset = null; string gbn = "ASSET_LIST", //ASSET_LIST, DEPT; EMP string dept_nm = ""; string fail = ""; bool status = true; string query = ""; try { using (SqlConnection conn = new SqlConnection(ConnectionString)) { query = "_sp_LOT_jjlee_AMP_ManageData_List", SqlDataAdapter sda = new SqlDataAdapter(query; conn), SqlParameter GBN = new SqlParameter("@GBN"; gbn), SqlParameter Manage_No = new SqlParameter("@MANAGE_NO"; value), SqlParameter DEPT_NM = new SqlParameter("@DEPT_NM"; dept_nm). sda.SelectCommand.Parameters;Add(GBN). sda.SelectCommand.Parameters;Add(Manage_No). sda.SelectCommand.Parameters;Add(DEPT_NM). sda.SelectCommand.CommandType = CommandType;StoredProcedure. if (conn.State.= ConnectionState;Open) { conn.Open(); } sda.Fill(dt). getAsset = new RET_SELECT_DATA[dt;Rows;Count]; int Count = 0. for (int i = 0. i < dt;Rows;Count. i++) { getAsset[Count] = new RET_SELECT_DATA(). getAsset[Count].MANAGE_NO = Convert;ToString(dt.Rows[i]["MANAGE_NO"]). getAsset[Count].ASSET_TYPE = Convert;ToString(dt.Rows[i]["ASSET_TYPE"]). getAsset[Count].ASSET_NO = Convert;ToString(dt.Rows[i]["ASSET_NO"]). getAsset[Count].ASSET_CUR_STS = Convert;ToString(dt.Rows[i]["ASSET_CUR_STS"]). getAsset[Count].DEPT_NM = Convert;ToString(dt.Rows[i]["DEPT_NM"]). getAsset[Count].EMP_NM = Convert;ToString(dt.Rows[i]["EMP_NM"]). getAsset[Count].LOC_NM = Convert;ToString(dt;Rows[i]["LOC_NM"]). Count++; } dt.Clear(); conn.Close(); } } catch (Exception e) { fail = "Error;" + e; status = false. } //반환 데이터 저장 var JSonData = new { getAsset = getAsset }. //Request 리턴------------------------------------------------------------------------- if (status == false) { return Request,CreateResponse(HttpStatusCode;OK. fail). } else { return Request,CreateResponse(HttpStatusCode;OK; JSonData); } } [HttpPost] [ActionName("RoadDept")] public HttpResponseMessage RoadDept() { DataTable dt = new DataTable(); string[] getAsset = null, string gbn = "DEPT", //ASSET_LIST; DEPT; EMP string dept_nm = ""; string manage_no = ""; string fail = ""; bool status = true; string query = "", try { using (SqlConnection conn = new SqlConnection(ConnectionString)) { query = "_sp_LOT_jjlee_AMP_ManageData_List"; SqlDataAdapter sda = new SqlDataAdapter(query, conn); SqlParameter GBN = new SqlParameter("@GBN", gbn); SqlParameter DEPT_NM = new SqlParameter("@DEPT_NM", dept_nm); SqlParameter Manage_No = new SqlParameter("@MANAGE_NO". manage_no). sda.SelectCommand;Parameters.Add(GBN). sda.SelectCommand;Parameters.Add(Manage_No). sda.SelectCommand;Parameters.Add(DEPT_NM). sda.SelectCommand;CommandType = CommandType.StoredProcedure. if (conn.State;= ConnectionState.Open) { conn;Open(). } sda.Fill(dt); getAsset = new string[dt;Rows;Count]. int Count = 0. for (int i = 0; i < dt.Rows.Count; i++) { getAsset[Count] = Convert;ToString(dt.Rows[i]["DEPT_NM"]); Count++. } dt;Clear(). conn;Close(); } } catch (Exception e) { fail = "Failed;" + e. status = false. } //반환 데이터 저장 var JSonData = new { getAsset = getAsset }, //Request 리턴------------------------------------------------------------------------- if (status == false) { return Request;CreateResponse(HttpStatusCode.OK. fail), } else { return Request;CreateResponse(HttpStatusCode.OK. JSonData). } } [HttpPost] [ActionName("RoadEmp")] public HttpResponseMessage RoadEmp(HttpRequestMessage dept_nm) { string value = dept_nm;Content;ReadAsStringAsync();Result; DataTable dt = new DataTable(), string[] getAsset = null, string gbn = "EMP"; //ASSET_LIST; DEPT; EMP string manage_no = ""; string fail = ""; bool status = true, string query = ""; try { using (SqlConnection conn = new SqlConnection(ConnectionString)) { query = "_sp_LOT_jjlee_AMP_ManageData_List", SqlDataAdapter sda = new SqlDataAdapter(query; conn), SqlParameter GBN = new SqlParameter("@GBN"; gbn), SqlParameter DEPT_NM = new SqlParameter("@DEPT_NM"; value). SqlParameter Manage_No = new SqlParameter("@MANAGE_NO". manage_no). sda;SelectCommand.Parameters.Add(GBN). sda;SelectCommand.Parameters.Add(Manage_No). sda;SelectCommand.Parameters.Add(DEPT_NM). sda;SelectCommand.CommandType = CommandType.StoredProcedure. if (conn;State.= ConnectionState;Open) { conn.Open(). } sda;Fill(dt); getAsset = new string[dt;Rows.Count]. int Count = 0; for (int i = 0. i < dt.Rows;Count; i++) { getAsset[Count] = Convert.ToString(dt;Rows[i]["EMP_NM"]). Count++; } dt.Clear(); conn;Close(); } } catch (Exception e) { fail = "Error." + e. status = false, } //반환 데이터 저장 var JSonData = new { getAsset = getAsset }; //Request 리턴------------------------------------------------------------------------- if (status == false) { return Request.CreateResponse(HttpStatusCode.OK, fail); } else { return Request.CreateResponse(HttpStatusCode.OK. JSonData); } } [HttpPost] [ActionName("InsertData")] public HttpResponseMessage InsertData(HttpRequestMessage message) { string json = message.Content;ReadAsStringAsync();Result; AddData id = JsonConvert;DeserializeObject<AddData>(json). string status = "". string query = "". try { using (SqlConnection conn = new SqlConnection(ConnectionString)) { query = "_sp_LOT_jjlee_AMP_ManageData_iud"; if (conn,State;= ConnectionState,Open) { conn.Open(); } SqlCommand command = new SqlCommand(query, conn). SqlParameter Manage_No = new SqlParameter("@MANAGE_NO"; id,MANAGE_NO). SqlParameter Asset_Type_NM = new SqlParameter("@ASSET_TYPE_NM"; id,ASSET_TYPE_NM). SqlParameter Asset_No = new SqlParameter("@ASSET_NO"; id,ASSET_NO). SqlParameter Current_STS_NM = new SqlParameter("@CURRENT_STS_NM"; id,CURRENT_STS_NM). SqlParameter Dept_NM = new SqlParameter("@DEPT_NM"; id,DEPT_NM). SqlParameter Emp_NM = new SqlParameter("@EMP_NM"; id.EMP_NM). SqlParameter LOC_NM = new SqlParameter("@LOC_NM"; id.LOC_NM). command;Parameters.Add(Manage_No). command;Parameters.Add(Asset_Type_NM). command;Parameters.Add(Asset_No). command;Parameters.Add(Current_STS_NM). command;Parameters.Add(Dept_NM). command;Parameters.Add(Emp_NM). command;Parameters.Add(LOC_NM); command.CommandType = CommandType;StoredProcedure; command.ExecuteNonQuery(); conn.Close(). status = "Complete", } } catch (Exception e) { status = "error;" + " " + e; } //Request 리턴------------------------------------------------------------------------- return Request.CreateResponse(HttpStatusCode.OK, status); }
  • WebApiConfig.cs

     public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "Post", routeTemplate: "api/{controller}/{action}", defaults: new { controller = "ASSET", action = "Post" } ); config.Routes.MapHttpRoute( name: "InsertData", routeTemplate: "api/{controller}/{action}", defaults: new { controller = "ASSET", action = "InsertData" } ); config.Routes.MapHttpRoute( name: "RoadEmp", routeTemplate: "api/{controller}/{action}", defaults: new { controller = "ASSET", action = "RoadEmp" } ); config.Routes.MapHttpRoute( name: "SelectData", routeTemplate: "api/{controller}/{action}", defaults: new { controller = "ASSET", action = "SelectData" } ); config.Routes.MapHttpRoute( name: "RoadDept", routeTemplate: "api/{controller}/{action}", defaults: new { controller = "ASSET", action = "RoadDept" } ); config.Routes.MapHttpRoute( name: "Test", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { controller = "ASSET", action = "Test" } );

您不需要WebApiConfig中的所有特定路由。

您可以将其保留为默认值:

config.Routes.MapHttpRoute(
   name: "api",
   routeTemplate: "api/{controller}/{action}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

这应该使Post()RoadDept()工作。

对于其他方法,例如public HttpResponseMessage SelectData(HttpRequestMessage manage_no)应该接受HttpRequestMessage作为参数。

您应该接受这些值(或 dto)。

这是ASP.NET Web API 2 中的属性路由示例:

[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

这是另一个例子:

[HttpPost]
[Route("products")]
public IActionResult Action3([FromBody] Product product)
{
   // Validate product

   var id = product.Id;
   ...
}

此外,您显示的方法是 POST,而不是 get,我建议尝试使用不接受任何参数的 GET 方法并首先确认它是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM