Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC5中使用管线属性_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5_Asp.net Mvc Routing - Fatal编程技术网

C# 在MVC5中使用管线属性

C# 在MVC5中使用管线属性,c#,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,首先,我阅读和阅读 我想我还有一个问题。因为一切都是一样的 我有MVC5项目。我有两个领域。 首先,我的默认根类如下所示 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{

首先,我阅读和阅读 我想我还有一个问题。因为一切都是一样的

我有MVC5项目。我有两个领域。 首先,我的默认根类如下所示

public static void RegisterRoutes(RouteCollection routes)
{
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
    defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional});
}
第一区域路由配置。

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "App_default",
        "App/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}
在这一点上,没有问题。但我想这样:

此应用程序/控制器

public class AccountsController : Controller
{
    // GET: App/Accounts
    [Route("app/accounts/list/{Id}")]
    public ActionResult List()
    {
        return View();
    }
}
现在,我可以访问如下内容:

  • localhost/Index/Index正常
  • localhost/App/Index/Index正常
  • localhost/app/accounts/list/45646错误

如何使用区域路由属性?我做不到?

要使“localhost/app/accounts/List/45646”URL正常工作,您需要在列表ActionResult中接收一个整数作为参数

public class AccountsController : Controller
 {
     // GET: App/Accounts
     [Route("app/accounts/list/{Id}")]
     public ActionResult List(int Id)
     {

          return View();
      }
  }

首先,需要启用属性路由

public static void RegisterRoutes(RouteCollection routes) {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      //Enable attribute routing
      routes.MapMvcAttributeRoutes();

      //Area registration should be done after
      //attribute routes to avoid route conflicts
      AreaRegistration.RegisterAllAreas();

      //convention-based routing
      routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Welcome", action = "Index", id = UrlParameter.Optional}
      );
}
然后将适当的属性添加到控制器

[RouteArea("AreaName", AreaPrefix = "app/accounts")]
public class AccountsController : Controller {
    [HttpGet]
    [Route("list/{id:int}")] // Matches GET app/accounts/list/45646
    public ActionResult List(int id) {
        return View();
    }
}
[路由(“app/accounts/list/{Id}”)]
更改为
[路由(“app/accounts/list/{Id}”)]