日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject...

發(fā)布時間:2025/3/18 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

希望實(shí)現(xiàn)的效果是:對購物車中所有商品的總價,實(shí)現(xiàn)9折或8折:

?

?

當(dāng)點(diǎn)擊"9折":

?

?

當(dāng)點(diǎn)擊"8折":

?

□ 思路

8折或9折是打折接口的不同實(shí)現(xiàn),關(guān)鍵是:由什么條件決定使用哪種打折方式?

--當(dāng)點(diǎn)擊8折或9折鏈接的時候,把參數(shù)放在路由中,然后在自定義控制器工廠中根據(jù)參數(shù)的不同選擇使用哪種打折方式。

?

□ model

??? public class CartLine
??? {
??????? public int Id { get; set; }
??????? public string Name { get; set; }
??????? public decimal Price { get; set; }
??????? public int Quantity { get; set; }
??? }

?

□ 接口

using MvcApplication2.Models;

namespace MvcApplication2
{
??? public interface IDiscount
??? {
??????? decimal GetFinalPrice(List<CartLine> cartLines);
??? }
}

?

□ 接口的2種實(shí)現(xiàn)

using System.Collections.Generic; ? namespace MvcApplication2.implementation { public class NineDiscount : IDiscount { public decimal GetFinalPrice(List<Models.CartLine> cartLines) { decimal result = 0.0M; foreach (var item in cartLines) { result += item.Price*item.Quantity; } return result*(90M/100M); } } } ? ? ? using System.Collections.Generic; ? namespace MvcApplication2.implementation { public class EightDiscount : IDiscount { public decimal GetFinalPrice(List<Models.CartLine> cartLines) { decimal result = 0.0M; foreach (var item in cartLines) { result += item.Price * item.Quantity; } return result * (80M / 100M); } } } ?

?

□ HomeController

using System.Collections.Generic; using System.Web.Mvc; using MvcApplication2.Models; ? namespace MvcApplication2.Controllers { public class HomeController : Controller { public ActionResult Index() { List<CartLine> cartLines = new List<CartLine>() { new CartLine(){Id = 1, Name = "Product1", Price = 80M, Quantity = 2}, new CartLine(){Id = 2, Name = "Product2", Price = 100M, Quantity = 3}, }; Session["cart"] = cartLines; return View(cartLines); } ? } } ?

?

□ Home/Index.cshtml

把不同的打折方式放在路由中傳遞。

@model List<MvcApplication2.Models.CartLine> ? @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ? <h2>Index</h2> ? <table> <tr style="background-color: #e3e3e3;"> <td>產(chǎn)品</td> <td>價格</td> <td>數(shù)量</td> </tr> @foreach (var item in Model) { <tr> <td>@item.Name</td> <td>@string.Format("{0:C}", item.Price)</td> <td>@item.Quantity</td> </tr> } </table> ? <p> @Html.ActionLink("9折購買", "Index", "Shop", new {policy = "Nine"},new {}) </p> ? <p> @Html.ActionLink("8折購買", "Index", "Shop", new {policy = "Eight"},new {}) </p> ?

?

□ 自定義控制器工廠,使用Ninject,根據(jù)路由參數(shù)policy的不同,決定選擇具體的打折接口實(shí)現(xiàn)

using System; using System.Web.Mvc; using System.Web.Routing; using MvcApplication2.implementation; using Ninject; ? namespace MvcApplication2.Extension { public class NinjectControllerFactory : DefaultControllerFactory { IKernel ninjectKernel; string policy = ""; ? public NinjectControllerFactory() { ninjectKernel = new StandardKernel(); } ? protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (requestContext.RouteData.Values["policy"] != null) { policy = requestContext.RouteData.Values["policy"].ToString(); } AddBindings(); return controllerType == null ? null : (IController) ninjectKernel.Get(controllerType); } ? private void AddBindings() { switch (policy) { case "Eight": ninjectKernel.Rebind<IDiscount>().To<EightDiscount>(); break; case "Nine": ninjectKernel.Rebind<IDiscount>().To<NineDiscount>(); break; default: ninjectKernel.Rebind<IDiscount>().To<NineDiscount>(); break; } } } } ?

?

□ 自定義控制器工廠全局注冊

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

?

□ ShopController中使用打折接口方法

using System; using System.Collections.Generic; using System.Web.Mvc; using MvcApplication2.Models; ? ? namespace MvcApplication2.Controllers { public class ShopController : Controller { public IDiscount _Discount; ? public ShopController(IDiscount discount) { this._Discount = discount; } ? public ActionResult Index(string policy) { List<CartLine> cartLines = new List<CartLine>(); if (Session["cart"] != null) { cartLines = (List<CartLine>)Session["cart"]; } ViewData["total"] = String.Format("{0:C}",_Discount.GetFinalPrice(cartLines)); return View(); } ? } } ?

?

□ Shop/Index.cshtml

@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ? 打折后的價格為: @ViewData["total"]


□ 自定義路由

為了讓url更直觀,符合controller/action/paramter:

routes.MapRoute( name: "Default", url: "{controller}/{action}/{policy}", defaults: new { controller = "Home", action = "Index", policy = UrlParameter.Optional } ); 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的MVC扩展控制器工厂,通过继承DefaultControllerFactory来决定使用哪个接口实现,使用Ninject...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。