多个控制器,一个视图和一个模型ASP.NET MVC 3
|
我想要一个型号和一个我的ASP.NET MVC 3应用程序中由多个控制器提供的视图. 我正在实现一个与用户的在线日历交互的系统,我支持Exchange,Google,Hotmail,Yahoo,Apple等等……每个都有不同的日历API实现,但我可以用我的自己的模特.我想通过在控制器级实现多态,我将能够干净地处理不同的API和身份验证问题. 我有一个很好的清洁模型和视图,到目前为止我已经实现了两个控制器,证明我可以读取/查询/写入/更新到Exchange和Google:ExchangeController.cs和GoogleController.cs. 我有/ Views / Calendar包含我的视图代码.我还有包含我的模型的/Models/CalendarModel.cs. 我想测试用户在我的ControllerFactory中使用哪个日历系统.我已经实现了这样: public class CustomControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext,Type controllerType)
{
if (controllerType == typeof(CalendarController))
{
if(MvcApplication.IsExchange) // hack for now
return new ExchangeController();
else
return new GoogleController();
}
return base.GetControllerInstance(requestContext,controllerType);
}
}
在我的Application_Start中: ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory()); 这有效.如果我到http://…/Calendar这个工厂代码工作,并创建正确的控制器! 这很好用,我没有真正理解我在做什么.现在我想我得到了它但我想确保我没有错过任何东西.我真的花时间寻找这样的东西而没有找到任何东西. 我担心的一件事是,我认为我可以在CalendarController和ExchangeController / GoogleController之间建立一个继承关系,如下所示: public class ExchangeController : CalendarController
{
但如果我这样做,我得到: The current request for action 'Index' on controller type 'GoogleController' is ambiguous between the following action methods: System.Web.Mvc.ViewResult Index(System.DateTime,System.DateTime) on type Controllers.GoogleController System.Web.Mvc.ActionResult Index() on type Controllers.CalendarController 哪个让我失望,因为我想在基础上放置一些常用的功能,现在我想我将不得不采用另一种方式. 这是为一个视图/模型设置多个控制器的正确方法吗?还有什么我需要考虑的? 编辑:关于我的impl的更多细节 根据下面的回答(谢谢!)我想我需要展示更多代码,以确保你们看到我正在尝试做的事情.我的模型实际上只是一个数据模型.它从这开始: /// <summary>
/// Represents a user's calendar across a date range.
/// </summary>
public class Calendar
{
private List<Appointment> appointments = null;
/// <summary>
/// Date of the start of the calendar.
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// Date of the end of the calendar
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// List of all appointments on the calendar
/// </summary>
public List<Appointment> Appointments
{
get
{
if (appointments == null)
appointments = new List<Appointment>();
return appointments;
}
set { }
}
}
然后我的控制器有以下方法: public class ExchangeController : Controller
{
//
// GET: /Exchange/
public ViewResult Index(DateTime startDate,DateTime endDate)
{
// Exchange specific gunk. The MvcApplication._service thing is a temporary hack
CalendarFolder calendar = (CalendarFolder)Folder.Bind(MvcApplication._service,WellKnownFolderName.Calendar);
Models.Calendar cal = new Models.Calendar();
cal.StartDate = startDate;
cal.EndDate = endDate;
// Copy the data from the exchange object to the model
foreach (Microsoft.Exchange.WebServices.Data.Appointment exAppt in findResults.Items)
{
Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service,exAppt.Id);
Models.Appointment appt = new Models.Appointment();
appt.End = a.End;
appt.Id = a.Id.ToString();
...
}
return View(cal);
}
//
// GET: /Exchange/Details/5
public ViewResult Details(string id)
{
...
Models.Appointment appt = new Models.Appointment();
...
return View(appt);
}
//
// GET: /Exchange/Edit/5
public ActionResult Edit(string id)
{
return Details(id);
}
//
// POST: /Exchange/Edit/5
[HttpPost]
public ActionResult Edit(MileLogr.Models.Appointment appointment)
{
if (ModelState.IsValid)
{
Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service,new ItemId(appointment.Id));
// copy stuff from the model (appointment)
// to the service (a)
a.Subject = appointment.Subject
...
a.Update(ConflictResolutionMode.AlwaysOverwrite,SendInvitationsOrCancellationsMode.SendToNone);
return RedirectToAction("Index");
}
return View(appointment);
}
//
// GET: /Exchange/Delete/5
public ActionResult Delete(string id)
{
return Details(id);
}
//
// POST: /Exchange/Delete/5
[HttpPost,ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{
Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service,new ItemId(id));
a.Delete(DeleteMode.MoveToDeletedItems);
return RedirectToAction("Index");
}
所以它基本上是典型的CRUD东西.我已经提供了ExchangeCalendar.cs版本的示例. GoogleCalendar.cs在实现方面显然相似. 我的模型(日历)和相关的类(例如约会)是从控制器传递到视图的内容.我不希望我的观点看到正在使用的基础在线服务的详细信息.我不明白如何使用接口(或抽象基类)实现Calendar类将为我提供我正在寻找的多态性. 在某些地方我必须根据用户选择要使用的实现. 我可以这样做: >在我的模特中.我不想这样做,因为那时我的模型通过特定于服务的代码变得非常苛刻. 以下回复提到“服务层”.我想这也许是我离开轨道的地方.如果你看看MVC正常使用数据库的方式,dbContext代表“服务层”,对吧?那么也许你们所建议的是第四个我可以做间接的地方?例如,上面的编辑将是这样的: private CalendarService svc = new CalendarService( e.g. Exchange or Google );
//
// POST: /Calendar/Edit/5
[HttpPost]
public ActionResult Edit(MileLogr.Models.Appointment appointment)
{
if (ModelState.IsValid)
{
svc.Update(appointment);
return RedirectToAction("Index");
}
return View(appointment);
}
这是正确的方法吗? 对不起,这已经变得如此啰嗦,但这是我知道如何获得足够的背景的唯一方式…… 解决方法我想你在这里走的是一条危险的道路.控制器通常应该尽可能简单,并且只包含例如“胶水”之间的“胶水”.您的服务层和模型/视图.通过将一般日历抽象和供应商特定实现移出控制器,您可以摆脱路由和日历实现之间的耦合.编辑:我将在服务层实现多态,并在服务层中有一个工厂类检查您的用户数据库以查找当前用户的供应商并实例化CalendarService类的相应实现.这样就不需要检查控制器中的日历供应商,保持简单. 通过耦合到路线,我的意思是您的自定义网址是目前导致AFAICT出现问题的网址.通过使用单个控制器并将复杂性转移到服务层,您可以只使用MVC的默认路由. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET 5 API – Azure AD B2C
- asp.net-mvc – 如何在ActionFilterAttribute ASP MVC 5中为
- asp.net-membership – MiniProfiler和SqlMembershipProvid
- ASP.NET – 黑客死亡的黄色屏幕
- 使用ASP.NET MVC 3助手的标题属性的渲染跨度标签
- 通过asp.net 4.0中的“EnableViewState”和“ViewStateMode
- 在ASP.NET中构建动态“高级搜索”控件的建议
- 如果我的Asp.Net会话有IsNewSession == true,那我的意思是什
- asp.net-mvc – MVC 4提供的反伪造令牌是为用户“”而是当前
- asp.net-mvc – 如何使用Html.DropDownList为默认选项设置值
- asp.net – Ajax.ActionLink不工作,Response.IsA
- asp.net viewstate加密问题
- ASP.NET 5 HTML5历史
- asp.net-mvc – MVC Model Binding不适用于Kendo
- asp.net-mvc-4 – ASP.NET MVC电子商务解决方案(
- asp.net-mvc-3 – 剃刀中的部分的默认内容
- asp.net-mvc – 如何使用ASP.NET MVC实现自定义缓
- asp.net – Chrome网络标签中的延迟测量
- asp.net-mvc – Asp.net MVC:上传多个图像文件?
- 如何在实时服务器上调试asp.net应用程序
