相同的网址但不同的控制器?

问题描述:

是否有可能为同一个网址使用两个不同的控制器?相同的网址但不同的控制器?

这是需要的,因为我需要的URL始终保持不变,但它应该使用不同的控制器。我的控制器(Apples,Bananas等)和视图分为各自的项目。

我需要在我的主MVC项目中执行一个操作,以根据某些逻辑从BananasApples项目返回操作/视图。

那么,我将如何继续总是有相同的网址,但从不同的控制器返回操作/视图?

我使用MVC 4

+0

当用户下载我的样本输入网址进入他的浏览器,如果URL是相同的:你如何期望MVC选择正确的控制器? – 2013-03-18 09:22:58

+1

您可以使用一个控制器并发送类似于?type = apple或?type = bananas的参数,然后在控制器重定向到其他控制器的操作方法 – 2013-03-18 09:32:58

+0

@Øyvindurl路由到主项目中的某个操作,并且该操作应该有一些逻辑找出哪个控制器/操作返回。 – 2013-03-18 09:48:52

您的网址应该是一个选择控制器的逻辑。也许您需要重新组织您的项目以拥有一个控制器,并将其他逻辑放在控制器操作中以填充模型?

但是,如果您坚持走这条路线,您可能需要在DefaultControllerFactory中覆盖CreateController,这是实例化控制器的类,通常基于您的控制器名称。这里是我的一个项目为例:

public class ErrorHandlingControllerFactory : DefaultControllerFactory 
{ 
    /// <summary> 
    /// Injects a custom attribute 
    /// on every action that is invoked by the controller 
    /// </summary> 
    /// <param name="requestContext">The request context</param> 
    /// <param name="controllerName">The name of the controller</param> 
    /// <returns>An instance of a controller</returns> 
    public override IController CreateController(
     RequestContext requestContext, 
     string controllerName) 
    { 
     var controller = 
      base.CreateController(requestContext, 
      controllerName); 

     var c = controller as Controller; 

     if (c != null) 
     { 
      c.ActionInvoker = 
       new ErrorHandlingActionInvoker(
        new HandleErrorWithELMAHAttribute()); 
     } 

     return controller; 
    } 
} 

你需要设置你的路线达通的已知控制器名称(可怕的魔弦...),测试该控制器的名称,如果检测到运行您的逻辑来获取实际的控制器名称并将其传递到base.CreateController

我写了这些代码。我希望它能帮助你。我使用隐藏字段来了解哪种方法可以运行。

这些都是我的模型:

namespace MvcSameController.Models 
{ 
    public class RouteModel 
    { 
     public SampleModel1 SampleModel1 { get; set; } 
     public SampleModel2 SampleModel2 { get; set; } 
    } 

    public class SampleModel1 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 

    } 

    public class SampleModel2 
    { 
     public int Id { get; set; } 
     public string Surname { get; set; } 

    } 
} 

这是控制器:

using System.Web.Mvc; 
using MvcSameController.Models; 

namespace MvcSameController.Controllers 
{ 
    public class SameController : Controller 
    { 
     // 
     // GET: /Same/ 

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

     [HttpPost] 
     public void Index(RouteModel routeModel, string type) 
     { 

      if (type == "1") 
      { 
       //Code for type 1 
      } 

      else if (type == "2") 
      { 
       //Code for type 2 
      } 

     } 
    } 
} 

和看法:

@{ 
    ViewBag.Title = "Index"; 
} 
@model MvcSameController.Models.RouteModel 


    <section id="loginForm"> 
     <h2>Type1 </h2> 
     @using (Html.BeginForm()) 
     { 
      @Html.AntiForgeryToken() 
      @Html.ValidationSummary(true) 

      @Html.Hidden("type",1) 

      <fieldset> 
       <legend>Type1 Form</legend> 
       <ol> 
        <li> 
         @Html.LabelFor(m => m.SampleModel1.Name) 
         @Html.TextBoxFor(m => m.SampleModel1.Name) 
         @Html.ValidationMessageFor(m => m.SampleModel1.Name) 
        </li> 
       </ol> 
       <input type="submit" value="Run Method1" /> 
      </fieldset> 
     } 
    </section> 

    <section id="loginForm"> 
     <h2>Type2</h2> 
     @using (Html.BeginForm()) 
     { 
      @Html.AntiForgeryToken() 
      @Html.ValidationSummary(true) 
      @Html.Hidden("type",2) 
      <fieldset> 
       <legend>Type2 Form</legend> 
       <ol> 
        <li> 
         @Html.LabelFor(m => m.SampleModel2.Surname) 
         @Html.TextBoxFor(m => m.SampleModel2.Surname) 
         @Html.ValidationMessageFor(m => m.SampleModel2.Surname) 
        </li> 
       </ol> 
       <input type="submit" value="Run Method2" /> 
      </fieldset> 

     } 
    </section> 

你可以从here