آشنایی با نحوهی وهله سازی کنترلرها در ASP.NET MVC با ساخت یک Controller Factory سفارشی
نویسنده: کیانی مقدم علیرضا
تاریخ: ۱۳۹۴/۰۸/۱۲ ۲۰:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace ControllerExtensibility.Models
{
public class Result
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
}
} @model ControllerExtensibility.Models.Result
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Result</title>
</head>
<body>
<div>Controller: @Model.ControllerName</div>
<div>Action: @Model.ActionName</div>
</body>
</html> using ControllerExtensibility.Models;
using System.Web.Mvc;
namespace ControllerExtensibility.Controllers
{
public class ProductController : Controller
{
public ViewResult Index()
{
return View("Result", new Result
{
ControllerName = "Product",
ActionName = "Index"
});
}
public ViewResult List()
{
return View("Result", new Result
{
ControllerName = "Product",
ActionName = "List"
});
}
}
} using System.Web.Mvc;
namespace ControllerExtensibility.Controllers
{
public class CustomerController : Controller
{
public ViewResult Index()
{
return View("Result", new Result
{
ControllerName = "Customer",
ActionName = "Index"
});
}
public ViewResult List()
{
return View("Result", new Result
{
ControllerName = "Customer",
ActionName = "List"
});
}
}
} using System.Web.Routing;
using System.Web.SessionState;
namespace System.Web.Mvc
{
public interface IControllerFactory
{
IController CreateController(RequestContext requestContext,
string controllerName);
SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext,
string controllerName);
void ReleaseController(IController controller);
}
} using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using ControllerExtensibility.Controllers;
namespace ControllerExtensibility.Infrastructure
{
public class CustomControllerFactory : IControllerFactory
{
public IController CreateController(RequestContext requestContext,
string controllerName)
{
Type targetType = null;
switch (controllerName)
{
case "Product":
targetType = typeof (ProductController);
break;
case "Customer":
targetType = typeof (CustomerController);
break;
default:
requestContext.RouteData.Values["controller"] = "Product";
targetType = typeof (ProductController);
break;
}
return targetType == null
? null
: (IController) DependencyResolver.Current.GetService(targetType);
}
public SessionStateBehavior GetControllerSessionBehavior(RequestContext
requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
} | نام | نوع | توضیحات |
| HttpContext | HttpContextBase | حاوی اطلاعاتی در خصوص درخواست است. |
| RouteData | RouteData | حاوی اطلاعاتی در خصوص Rout است که با درخواست رسیده همخوانی دارد. |
default: requestContext.RouteData.Values["controller"] = "Product"; targetType = typeof(ProductController); break;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using ControllerExtensibility.Infrastructure;
namespace ControllerExtensibility
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new
CustomControllerFactory());
}
}
}