基础控制器获得价值

时间:2010-02-02 10:02:00

标签: asp.net-mvc

我无法获取基本控制器的值。我想做的就是让我的基本控制器从ActionLink中获取一个ID吗?

链接

<%= Html.ActionLink("About Us", "About", new { SectionId = 1 })%>

基本控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Website.Controllers
{
    public class SectionController : Controller
    {
        //
        // GET: /Section/

        public SectionController(int SectionId)
        {
            if (SectionId == 1)
            {
                ViewData["Message"] = "GOT AN ID";
            }
            else
            {
                ViewData["Message"] = "NO ID";
            }
        }
    }
}

家庭控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Website.Controllers
{
    [HandleError]
    public class HomeController : SectionController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }


        public ActionResult Contact()
        {
            return View();
        }

   }
}
到目前为止

解决方案

ActionLink的

<%= Html.ActionLink("About Us", "About", new { SectionId = 1})%>

SectionAttribute.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Website.ActionFilters
{
    public class SectionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
        // Actions have sectionId parameter
        object SectionId = filterContext.ActionParameters.FirstOrDefault(x => x.Key == "SectionId").Value;

            if (SectionId != null && (int)SectionId == 1)
            {
                filterContext.Controller.ViewData["Message"] = "GOT AN ID";
            }
            else
            {
                filterContext.Controller.ViewData["Message"] = "NO ID";
            }
        }
    }
}

SectionController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.ActionFilters;

namespace Website.Controllers
{
    [Section]
    public class SectionController : Controller
    {
    }
}

查看

<%= Html.Encode(ViewData["Message"]) %>

2 个答案:

答案 0 :(得分:2)

您的代码无效。相反,您应该在基本控制器中定义OnActionExecuting方法,从路径数据中获取实际的sectionId并相应地设置ViewData。尝试这样的事情(未经测试):

public class SectionController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Actions have no sectionId parameter - look for it in querystring
        string SectionId = filterContext.HttpContext.Request.QueryString["sectionId"];

        int sId;

        if (int.TryParse(SectionId, out sId) && sID == 1)
        {
            filterContext.Controller.ViewData["Message"] = "GOT AN ID";
        }
        else
        {
            filterContext.Controller.ViewData["Message"] = "NO ID";
        }
    }
}

<强>更新

您可以从控制器移出它并创建ActionFilter。这很简单:

public class SectionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Actions have sectionId parameter
        object SectionId = filterContext.ActionParameters
            .FirstOrDefault(x => x.Key == "sectionId").Value;

        if (SectionId != null && (int)SectionID == 1)
        {
            filterContext.Controller.ViewData["Message"] = "GOT AN ID";
        }
        else
        {
            filterContext.Controller.ViewData["Message"] = "NO ID";
        }
    }
}

...

[Section]
public class SectionController : Controller {

or

[HandleError, Section]
public class HomeController : SectionController {

答案 1 :(得分:0)

基本控制器的构造函数通常不是放置公共代码的位置。您通常会为公共代码创建一个过滤器。如果使用此过滤器属性修饰基本控制器,则所有继承控制器都将使用该过滤器。