1

It is my first time Web programming in ASP.NET. I'm trying to setting URL route on WebApiConfig.cs each method. but methods with the same parameter type is show a 404 error According to the order of registers in WebApiConfig.cs. This problem occurs even though the action name is set above the method. Why can't the url path be found depending on the location of the register function? I am also curious about the difference between WebApiConfig.cs and RouteConfig.cs. Thank you for reading the long article.

  • Controllers.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" }
           );
    

1 Answer 1

1

You don't need all the specific routes in WebApiConfig.

You could leave it as the default:

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

This should make Post() and RoadDept() work.

For the other methods e.g. public HttpResponseMessage SelectData(HttpRequestMessage manage_no) you should not be accepting HttpRequestMessage as an argument.

You should be accepting the values (or a dto).

Here's an example from Attribute Routing in ASP.NET Web API 2:

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

Here's another example:

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

   var id = product.Id;
   ...
}

Also, your shown methods are POST, not get, I would recommend trying with a GET method that doesn't accept any parameters and confirm it works first.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, Is there any Button like 'Like' ?
You can upvote (the triange next to the big number) and if the answer solves your problem you can make the answer as (the big check mark) accepted. The author get 10 points for an upvote and 15 for acceptance. meta.stackoverflow.com/questions/269653/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.