从具有多个参数作为数据的请求处理程序调用 ASP.NET MVC 控制器

发布于 2024-08-30 00:13:05 字数 2399 浏览 5 评论 0原文

我正在致力于为 CMS 系统构建 MVC 前端。 CMS 系统将为 ASP.NET MVC 提供页面和内容。

在 Global.asax 中,我注册了一个如下所示的自定义路由处理程序:

public class MvcApplication : EPiServer.Global
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new Route("{*data}", new MvcRouteHandler()));
    }

    protected void Application_Start()
    {
        ControllerBuilder.Current.SetControllerFactory(new MvcControllerFactory());
        ModelBinders.Binders.Add(typeof(MvcPageData), new PageDataModelBinder());
        RegisterRoutes(RouteTable.Routes);
    }
}

这就是我的路由处理程序的样子:

public class MvcRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MvcRequestHandler(requestContext);
    }
}

我的 MVC 请求处理程序:

protected void ProcessRequest(HttpContext httpContext)
{
    List<string> pageParams;

    // Get the requested page from the CMS
    EPiServer.UrlBuilder internalUrlBuilder = GetInternalUrl(httpContext.Request.RawUrl, out pageParams);
    MvcPageData mvcPage =
        CurrentPageResolver.Instance.GetCurrentPage(internalUrlBuilder.QueryCollection["id"] ??
            string.Empty);

    string controllerName = mvcPage.ControllerName;
    if (pageParams.Count == 0)
    {
        mvcHandler.RequestContext.RouteData.Values.Add("action", "Index");
    }
    else
    {
        mvcHandler.RequestContext.RouteData.Values.Add("action", pageParams[0]);
    }

    mvcHandler.RequestContext.RouteData.Values.Add("controller", controllerName);
    mvcHandler.RequestContext.RouteData.Values["data"] = mvcPage; // This works fine, but I also want to add the remidning pageParams

    IController controller = ControllerBuilder.Current.
        GetControllerFactory().CreateController(mvcHandler.RequestContext, controllerName);

    controller.Execute(mvcHandler.RequestContext);
}

这就是控制器今天的样子:

public class StandardController : Controller
{
    public ActionResult Index(MvcPageData currentPage)
    {
        return View(currentPage);
    }
}

我的问题是我希望能够传递超过控制器的一个参数,因此我需要更改 mvcHandler.RequestContext.RouteData.Values["data"] 以包含某种参数列表。我已经搜索过但没有找到解决问题的方法,也许这真的很简单。生成的控制器可能看起来像这样:

public ActionResult Index(MvcPageData currentPage, int id, string name)

有人知道如何做到这一点吗?谢谢。

I am working on building an MVC frontend for a CMS system. The CMS system will serve ASP.NET MVC with pages and content.

In Global.asax I registered a custom route handler like this:

public class MvcApplication : EPiServer.Global
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new Route("{*data}", new MvcRouteHandler()));
    }

    protected void Application_Start()
    {
        ControllerBuilder.Current.SetControllerFactory(new MvcControllerFactory());
        ModelBinders.Binders.Add(typeof(MvcPageData), new PageDataModelBinder());
        RegisterRoutes(RouteTable.Routes);
    }
}

This is how my route handler looks like:

public class MvcRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MvcRequestHandler(requestContext);
    }
}

And my MVC request handler:

protected void ProcessRequest(HttpContext httpContext)
{
    List<string> pageParams;

    // Get the requested page from the CMS
    EPiServer.UrlBuilder internalUrlBuilder = GetInternalUrl(httpContext.Request.RawUrl, out pageParams);
    MvcPageData mvcPage =
        CurrentPageResolver.Instance.GetCurrentPage(internalUrlBuilder.QueryCollection["id"] ??
            string.Empty);

    string controllerName = mvcPage.ControllerName;
    if (pageParams.Count == 0)
    {
        mvcHandler.RequestContext.RouteData.Values.Add("action", "Index");
    }
    else
    {
        mvcHandler.RequestContext.RouteData.Values.Add("action", pageParams[0]);
    }

    mvcHandler.RequestContext.RouteData.Values.Add("controller", controllerName);
    mvcHandler.RequestContext.RouteData.Values["data"] = mvcPage; // This works fine, but I also want to add the remidning pageParams

    IController controller = ControllerBuilder.Current.
        GetControllerFactory().CreateController(mvcHandler.RequestContext, controllerName);

    controller.Execute(mvcHandler.RequestContext);
}

This is what a controller looks like today:

public class StandardController : Controller
{
    public ActionResult Index(MvcPageData currentPage)
    {
        return View(currentPage);
    }
}

My problem is that I want to be able to pass more than one parameter to the controller, so I will need to change the mvcHandler.RequestContext.RouteData.Values["data"] to contain some kind of list of parameters. I have searched but not found an solution to the problem, maybe it is really simple. The resulting controler might look something like this:

public ActionResult Index(MvcPageData currentPage, int id, string name)

Anyone knows how to do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-09-06 00:13:05

您可以尝试创建具有项目计数 pageParams.Count 的对象数组,并向其中添加 mvcPage 对象和 pageParams 中除第一个对象(即您的操作名称)之外的所有对象。

You can try to create array of objects with items count pageParams.Count and add to it the mvcPage object and all objects from pageParams except the first one (which is your action name).

挽袖吟 2024-09-06 00:13:05

我误解了模型绑定在 MVC 中的工作原理。我认为发送到控制器的所有数据都必须在调用controller.Execute之前在ProcessRequest中定义,但事实并非如此。

I had missunderstood how the model binding works in MVC. I thought all the data sent to the controller had to be defined in ProcessRequest before calling controller.Execute, which not is the case.

不如归去 2024-09-06 00:13:05

您是否尝试过在 Web.config 中注册处理程序?

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="yourApiUrlPrefix/*" 
        type="MvcRouteHandler, YourAssemblyName" />
    </httpHandlers>
  </system.web>
</configuration>

Have you tried registering the handler in the Web.config?

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="yourApiUrlPrefix/*" 
        type="MvcRouteHandler, YourAssemblyName" />
    </httpHandlers>
  </system.web>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文