MVC 3 - 新区域 - 404 错误 - 未找到资源 - 已尝试路由调试器
我有一个小型 MVC 3 应用程序 - 一个演示场地。 我有一个区域并且运行良好。
我刚刚添加了另一个区域,希望启动应用程序并且它可以工作 - 但不,404 - 找不到资源。
AreaRegistration 中的地图路线是默认的(就像我创建的第一个区域一样)。
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Postcard_default",
"Postcard/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
我尝试在其中添加特定的控制器,但什么也没有。
因此,我下载了 Phil Haack 的 RouteDebugger,并且在输入 http://server/Postcard/Create 时找到了我的路线(其中也是我想要到达的地方)
区域的结构
现在我的控制器
public class CreateController : Controller
{
private ILogger Logger { get; set; }
private ICardSender Emailer { get; set; }
private IOCCardRepository CardRepository { get; set; }
public CreateController(ILogger logger, ICardSender cardSender, IOCCardRepository repository)
{
this.Logger = logger;
this.Emailer = cardSender;
this.CardRepository = repository;
}
//
// GET: /Postcard/Create/
public ActionResult Index()
{
var model = new OCPostcardModel().Create();
return View(model);
}
:我已经删除了整个区域,再次尝试,还是不行。所以我在路线中添加了特定控制器(在 AreaRegistration 文件内)
context.MapRoute(
"Postcard_default",
"Postcard/{controller}/{action}/{id}",
new { controller = "Create", action = "Index", id = UrlParameter.Optional }
);
并且它的工作...我不知道为什么当我之前这样做时它不起作用,但现在是了。
但仍然很好奇,因为我在我看过的任何演示中都没有看到任何人将此控制器添加到路线中 - 而且我在其他区域中还没有得到它?
I have a small MVC 3 app - bit of a demo ground.
I have one area and thats been working fine.
I have just added another area expecting to just spin up the app and it work - but no, 404 - The resource cannot be found.
The map route in the AreaRegistration is the default (as is the first area i created).
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Postcard_default",
"Postcard/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
I have tried adding in a specific controller into this, but nothing.
So I downloaded Phil Haack's RouteDebugger and my route is found when typing in http://server/Postcard/Create (which is where I am trying to get too)
Structure of the Area
My controller
public class CreateController : Controller
{
private ILogger Logger { get; set; }
private ICardSender Emailer { get; set; }
private IOCCardRepository CardRepository { get; set; }
public CreateController(ILogger logger, ICardSender cardSender, IOCCardRepository repository)
{
this.Logger = logger;
this.Emailer = cardSender;
this.CardRepository = repository;
}
//
// GET: /Postcard/Create/
public ActionResult Index()
{
var model = new OCPostcardModel().Create();
return View(model);
}
NOW: I have since deleted the entire area, tried again it didn't work. So I added in the specific controller in the route (Inside AreaRegistration file)
context.MapRoute(
"Postcard_default",
"Postcard/{controller}/{action}/{id}",
new { controller = "Create", action = "Index", id = UrlParameter.Optional }
);
And its working...I don't know why it didn't work when I did this before, but it is now.
Still curious though as I've not seen anyone add in this controller into route in any of the demo's i've looked at - and I haven't got it in my other area?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我将控制器移动到区域但忘记更新命名空间时,我遇到了这个问题。控制器名称的范围为区域的命名空间。因此“Area”中的“Some”将映射到App.Areas.Area.Controllers.SomeController,而该控制器并不存在。
I ran into this when I moved a controller into an Area but forgot to update the namespace. The controller name is scoped to the Area's namespace. So "Some" in "Area" will map to App.Areas.Area.Controllers.SomeController, which didn't exist.
您的地图路线中缺少控制器部分
You were missing the controller part in your maproute
尝试在明信片区域下添加一个类 PostCardAreaRegistration
Try to add a class PostCardAreaRegistration under PostCard Area