我找到了很多关于这个的话题和建议,我想我已经尝试了所有的组合。
我有一个工作应用程序会变得更大,所以我决定利用区域。我放弃了它并且可以访问控制器,但是当它尝试将数据返回到视图时应用程序失败了。我创建了一个测试数据上下文、类等,然后确保在进入区域时这不是我做错的事情。
最好先完成较小的测试情况,然后将有效的方法应用于较大的实际应用程序。
我的链接来自_Layout.cshtml
:
<li><a href="#">@Html.ActionLink("Test", "Index", "testclasses")</a></li>
我的模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Tracking.Areas.test.Models
{
public class testclass
{
public int ID { get; set; }
}
}
我的测试区域注册:
using System.Web.Mvc;
namespace Tracking.Areas.test
{
public class testAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "test";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"test_default",
//"test/{controller}/{action}/{id}",
"test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
我的测试控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Tracking.Areas.test.Models;
using Tracking.DAL.Test;
namespace Tracking.Areas.test.Controllers
{
[RouteArea("test")]
public class testclassesController : Controller
{
private testcontext db = new testcontext();
// GET: test/testclasses
public async Task<ActionResult> Index()
{
return View(await db.spas.ToListAsync());
}
// GET: test/testclasses/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
testclass testclass = await db.spas.FindAsync(id);
if (testclass == null)
{
return HttpNotFound();
}
return View(testclass);
}
// GET: test/testclasses/Create
public ActionResult Create()
{
return View();
}
// POST: test/testclasses/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ID")] testclass testclass)
{
if (ModelState.IsValid)
{
db.spas.Add(testclass);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(testclass);
}
// GET: test/testclasses/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
testclass testclass = await db.spas.FindAsync(id);
if (testclass == null)
{
return HttpNotFound();
}
return View(testclass);
}
// POST: test/testclasses/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "ID")] testclass testclass)
{
if (ModelState.IsValid)
{
db.Entry(testclass).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(testclass);
}
// GET: test/testclasses/Delete/5
public async Task<ActionResult> Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
testclass testclass = await db.spas.FindAsync(id);
if (testclass == null)
{
return HttpNotFound();
}
return View(testclass);
}
// POST: test/testclasses/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
testclass testclass = await db.spas.FindAsync(id);
db.spas.Remove(testclass);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
我的 RouteConfig 文件
namespace Tracking
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
我的 Global.asax.cs 文件
namespace Tracking
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
我已经[RouteArea("test")]
在控制器中尝试过使用和不使用;我尝试了多种选择,包括将控制器添加到区域注册文件、从区域注册文件中的控制器前面删除区域名称以及许多其他组合。
当我在其中放置断点并到达它们时,代码会找到控制器和模型 - 当它尝试返回视图时,我收到有关在标准位置找不到索引视图的错误消息 - 它不在区域内搜索全部。
谢谢!