簡體   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