【问题标题】:skipping controller and action names mvc跳过控制器和动作名称 mvc
【发布时间】:2020-08-28 10:41:19
【问题描述】:

我正在使用 ASP.NET MVC 开发一个项目

我正在使用启用了 MVC 的 ASP.NET Web 应用程序

我有一个 Home 控制器,其中包含默认的 Index 操作,有一个我可以像这样访问的东西列表

site.com/?parameter=test1

但我希望能够像这样访问列表

site.com/test1

其中 test1 是参数,用作搜索参数。

我尝试了以下示例,但没有一个有效

 routes.MapRoute(
            name: "Default",
            url: "/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

还有这个

   routes.MapRoute(
            name: "Default",
            url: "{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    尝试使用操作上方的 [Route] 属性,并在启动时保留默认值。

    [Route("/{id?}"]
    public IActionResult Index(string id)
    {
      return View();
    }
    

    下面的网络框架使用

        public ActionResult Index(string id)
        {
            return View();
        }
    

    RouteConfig.cs

        routes.MapRoute(
       name: "Default",
       url: "{id}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                    );
    

    【讨论】:

    • 我正在使用“启用 MVC 的 ASP.NET Web 应用程序”我在尝试您的建议时收到此错误 “找不到类型或命名空间名称“IActionResult”(您是否缺少使用指令还是程序集引用?)”
    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多