RouteArea中AreaPrefix(Area 前缀)的使用

时间:2023-03-09 02:47:28
RouteArea中AreaPrefix(Area 前缀)的使用
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace WebApplication6.Controllers
{
[RouteArea("Admin")]
[RoutePrefix("testArea")]
[Route("{action}")] // 匹配 /dddAdmin/testArea/Index
public class MyAreaController : Controller
{
// GET: MyArea
public ActionResult Index()
{
return View();
}
}
}

这里只用了RouteArea,没有AreaPrefix ,后来我又试了一下加AreaPrefix 的

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace WebApplication6.Controllers
{
[RouteArea("Admin" ,AreaPrefix="ddd")]
[RoutePrefix("testArea")]
[Route("{action}")]
// 我想当然以为是匹配 /ddd/Admin/testArea/Index
public class MyAreaController : Controller
{
// GET: MyArea
public ActionResult Index()
{
return View();
}
}
}

结过不是想象的那样,因为是第一次用,网上没找到AreaPrefix 的用法,可能是找的方式不对,于是去看了msdn,

以下是对AreaPrefix的解释。

RouteArea中AreaPrefix(Area 前缀)的使用

看到后半句,我就猜想AreaPrefix 难道与区域名称是相互替代的???

我就试了一下

<a href="/ddd/testArea/Index"> Area测试  </a>

果然能访问到了,比较简单,用于自己备忘罢了