[尝试发布到控制器时收到404错误

问题描述 投票:0回答:1

我正在尝试通过2个参数调用我的控制器方法,但从未执行控制器操作,并且返回了404错误。

查看过其他类似的问题后,我尝试重新格式化操作链接,还尝试使用@ 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

asp.net-mvc actionlink
1个回答
0
投票

请选中此代码,您需要更改一些小代码。

在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
        }
    }

注意:当您在controller action中传递“ 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 }
            );
        }            
    }

Image

© www.soinside.com 2019 - 2024. All rights reserved.