ได้รับข้อผิดพลาด 404 เมื่อพยายามโพสต์ไปยังคอนโทรลเลอร์

ฉันกำลังพยายามเรียกใช้เมธอดคอนโทรลเลอร์ของฉันโดยส่งพารามิเตอร์ 2 ตัว แต่การกระทำของคอนโทรลเลอร์ไม่เคยถูกเข้าถึง และข้อผิดพลาด 404 ถูกส่งกลับ

เมื่อดูคำถามที่คล้ายกันอื่น ๆ ฉันได้ลองฟอร์แมต actionlink ใหม่แล้วลองใช้ @html.action ตรวจสอบให้แน่ใจว่ามันเป็น HttpGet แทนที่จะเป็น HttpPost และเห็นได้ชัดว่าทำให้วิธีการดำเนินการนั้นมีอยู่ในคอนโทรลเลอร์จริงๆ

ผลการดำเนินการ:

     @Html.ActionLink(
                   linkText: item.FileName,
                   actionName: "GetStatement",
                   controllerName: "Statements",
                   routeValues: new { id = item.Id, entityCode = 
    item.EntityCode },
                   htmlAttributes: null)

วิธีการควบคุม

public class StatementsController : Controller
    {
        [HttpGet]
        public ActionResult GetStatement(int id, int entityCode)
        {
           //go to repository and get statement
        }
    }

ฉันไม่แน่ใจด้วยว่า URL ที่เกี่ยวข้องนั้นมีรูปแบบที่ถูกต้อง: Statments/GetStatement/1234?entityCode=111


person CodeChunky    schedule 12.10.2019    source แหล่งที่มา


คำตอบ (1)


โปรดตรวจสอบอันนี้ซึ่งคุณจำเป็นต้องเปลี่ยนโค้ดเล็กๆ น้อยๆ

ในหน้า cshtml

 @Html.ActionLink(
                   linkText: item.FileName,
                   actionName: "GetStatement",
                   controllerName: "Statements",
                   routeValues: new { itemid = item.Id, entityCode = 
    item.EntityCode },
                   htmlAttributes: null)

รหัสตัวควบคุม

public class StatementsController : Controller
    {
        [HttpGet]
        public ActionResult GetStatement(int itemid, int entityCode)
        {
           //go to repository and get statement
        }
    }

หมายเหตุ: เมื่อคุณส่ง "Id" ใน การทำงานของคอนโทรลเลอร์ กำหนดเส้นทางการแปลงโดยอัตโนมัติในตัวอย่างด้านล่าง

public ActionResult HandleException(int id)
        {
            // id mentioned in **RouteConfig** file that's way URL automatic mapped
        }

ดูไฟล์ RouteConfig

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {                
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }            
    }

รูปภาพ

person jishan siddique    schedule 12.10.2019