MVC控制器操作参数为空

时间:2022-12-01 23:34:44

I have Controller name: District and Action name: Incharges But I want the URL to be like this (action name with some paremeter)

我有控制器名称:区域和操作名称:Incharges但我希望URL是这样的(带有一些paremeter的动作名称)

www.example.com/district/incharges/aaa

www.example.com/district/incharges/aaa

www.example.com/district/incharges/bbb

www.example.com/district/incharges/bbb

www.example.com/district/incharges/ccc

www.example.com/district/incharges/ccc

But, while debugging teamName always return as NULL in the action parameter.

但是,调试teamName时总是在action参数中返回NULL。

Routing

路由

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

            routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges" }
            ); 

Controller

调节器

But, while debugging teamName always return as NULL in the action parameter.

但是,调试teamName时总是在action参数中返回NULL。

public class DistrictController : Controller
    {     


        public ActionResult Incharges(string teamName)
        {
            InchargePresentationVM INPVM = new InchargePresentationVM();
            INPVM.InitializePath(teamName, string.Empty);
            return View("", INPVM);
        }
}

View

视图

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>

2 个解决方案

#1


17  

specific route you have to declare the first

具体路线你必须申报第一个

routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges", id = UrlParameter.Optional }

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

#2


1  

ASP.NET MVC DefaultModelBinder will try and do implicit type conversion of the values from your value provider , eg. form, to the action method parameters. If it tries to convert a type from the value provider to the parameter and it is not able to do it, it will assign null to the parameter.

ASP.NET MVC DefaultModelBinder将尝试对值提供程序中的值进行隐式类型转换,例如。 form,to action方法参数。如果它尝试将类型从值提供程序转换为参数并且无法执行此操作,则会为参数指定null。

Regarding routing, ASP.NET MVC has the concept of conversion over configuration. If you follow the conversion, then instead of configuration. You can keep your default route and always have the route you want by naming your controllers, action methods and parameter names.

关于路由,ASP.NET MVC具有转换优于配置的概念。如果您按照转换,那么而不是配置。您可以通过命名控制器,操作方法和参数名称来保留默认路由并始终拥有所需的路径。

With the convention over configuration you must keep the default HomeController which is the entry point of the application and then name other controllers as below. These can conform to the route names you want.

使用约定优于配置,您必须保留默认的HomeController,它是应用程序的入口点,然后命名其他控制器,如下所示。这些可以符合您想要的路线名称。

    namespace ConversionOverConfiguration
    {

      public class  DistrictController: Controller
      {
         public ActionResult Incharges(int aaa)
         {
            //You implementation here

             return View();
         }

      }

    }
The route will look as below if you have this conversion implementation
    //Controller/ActionMethod/ActionMethodParameter
    //District/Incharges/aaa

And this will give you domain URI:www.example.com/district/incharges/aaa . If action method parameter type is a string, then domain URI is:www.example.com/district/incharges/?aaa=name is a string. Then you can keep the ASP.NET MVC default routing

这将为您提供域URI:www.example.com/district/incharges/aaa。如果操作方法参数类型是字符串,则域URI为:www.example.com/district/incharges/?aaa = name是一个字符串。然后,您可以保留ASP.NET MVC的默认路由

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

#1


17  

specific route you have to declare the first

具体路线你必须申报第一个

routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges", id = UrlParameter.Optional }

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

#2


1  

ASP.NET MVC DefaultModelBinder will try and do implicit type conversion of the values from your value provider , eg. form, to the action method parameters. If it tries to convert a type from the value provider to the parameter and it is not able to do it, it will assign null to the parameter.

ASP.NET MVC DefaultModelBinder将尝试对值提供程序中的值进行隐式类型转换,例如。 form,to action方法参数。如果它尝试将类型从值提供程序转换为参数并且无法执行此操作,则会为参数指定null。

Regarding routing, ASP.NET MVC has the concept of conversion over configuration. If you follow the conversion, then instead of configuration. You can keep your default route and always have the route you want by naming your controllers, action methods and parameter names.

关于路由,ASP.NET MVC具有转换优于配置的概念。如果您按照转换,那么而不是配置。您可以通过命名控制器,操作方法和参数名称来保留默认路由并始终拥有所需的路径。

With the convention over configuration you must keep the default HomeController which is the entry point of the application and then name other controllers as below. These can conform to the route names you want.

使用约定优于配置,您必须保留默认的HomeController,它是应用程序的入口点,然后命名其他控制器,如下所示。这些可以符合您想要的路线名称。

    namespace ConversionOverConfiguration
    {

      public class  DistrictController: Controller
      {
         public ActionResult Incharges(int aaa)
         {
            //You implementation here

             return View();
         }

      }

    }
The route will look as below if you have this conversion implementation
    //Controller/ActionMethod/ActionMethodParameter
    //District/Incharges/aaa

And this will give you domain URI:www.example.com/district/incharges/aaa . If action method parameter type is a string, then domain URI is:www.example.com/district/incharges/?aaa=name is a string. Then you can keep the ASP.NET MVC default routing

这将为您提供域URI:www.example.com/district/incharges/aaa。如果操作方法参数类型是字符串,则域URI为:www.example.com/district/incharges/?aaa = name是一个字符串。然后,您可以保留ASP.NET MVC的默认路由

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