如何创建动态控制器来处理ASP.NET MVC中的许多静态视图?

时间:2016-03-13 15:24:07

标签: c# asp.net-mvc

我有一堆大多数静态页面(大约40个), 如:order-form01.html,order-form02.html,orderform03.html等..

他们每个人都有自己的控制器/动作,或者是否可以为所有人设置一个动态控制器/动作?

我的网址应如下所示:http://MyProject/GlobalController/IndividualView以及上述示例:http://MyProject/OrderForm/order-form01http://MyProject/OrderForm/order-form02等。

提前致谢。

2 个答案:

答案 0 :(得分:4)

是的,它非常简单并且您不需要switch语句或任何其他冗余逻辑。

public class MyController
{
  public ActionResult  Page(string file)
  {
    return View(file);
  }
}

魔术在路线图中:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // New MapRoute for your 40+ files..
        routes.MapRoute(
            "OrdeForm",
            "OrderForm/{file}",
            new { controller = "MyController", action = "Page", {file} = "" }
        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = "" }
        );

    }

此外:

  

我在查询字符串中传递了视图名称。

不是必需的,但是受支持。以下网址将起作用:

// Url Parameters
http://MyProject/OrderForm/order-form01
http://MyProject/OrderForm/order-form02

// Querystring Parameters
http://MyProject/OrderForm?file=order-form01
http://MyProject/OrderForm?file=order-form02

唯一的问题是您需要将html文件重命名为cshtml并将它们放在正确的目录中以供ViewEngine查找。

  @Erik,我对mvc也有点新意。您能否一次又一次地解释您的路线图,以及如何使用默认的raute

路由分为 3个值

Controller
Action
Parameter(s)

至少需要控制器和操作。值来自何处不依赖于Url。例如,在以下Url和Map Route ...

// Url
http://MyProject/

// MapRoute
routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Home", action = "Index", id = "" }
);

// Controller named "Home" matches the default in the above route
// Method named "Index" matches the default in the above route
public class HomeController {
  public ActionResult Index() {
    return new EmptyResult();
  }
}

...一切仍然有效,因为我们为控制器和操作提供了一个默认值。

好的,让我们分解你想要的网址:

http://MyProject/OrderForm/order-form01
http://MyProject/OrderForm/order-form02

http://MyProject/<identifier>/{parameter}

你有一个标识符告诉我路由( OrderForm )和一个因为它改变而你想要一个值的改变值应该是一个参数。

http://MyProject/<identifier>/{file}

参数的名称没有区别,只要它与控制器方法的签名匹配:

http://MyProject/{Controller}/{file}

public class HomeController {
  public ActionResult Index(string file) {
    return new EmptyResult();
  }
}

http://MyProject/{Controller}/{einstein}

public class HomeController {
  public ActionResult Index(string einstein) {
    return new EmptyResult();
  }
}

我将参数命名为 file ,因为它告诉我参数是文件的名称,而名称​​ einstein 没有固有的描述所以这是一个可怕的名字对于变量。

http://MyProject/{Controller}/{file}

// MapRoute
routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Home", action = "Index", id = "" }
);

// Controller named "Home" matches the default in the above route
// Method named "Index" matches the default in the above route
public class HomeController {
  public ActionResult Index() {
    return new EmptyResult();
  }
}

现在我们只希望此路由在标识符为OrderForm 时运行,因此我们不允许它成为值,我们会对其进行硬编码。

url: "OrderForm/...

接下来我们有一个不断变化的值,所以我们要添加url参数:

url: "OrderForm/{file}"

现在我们遇到了一个问题,因为我们不允许MVC从url中解析值以填充 Controller Action ,因此我们必须提供它们。

routes.MapRoute(
  name: "",
  url: "OrderForm/{file}",
  defaults: new { controller = "Home", action = "Index", file = "" }
);

我们已将网址http://MyProject/OrderForm/{file}映射到:

public class HomeController {
  public ActionResult Index(string file) {
    return new EmptyResult();
  }
}

现在我会选择将默认值更新为更具体和更具描述性的内容:

routes.MapRoute(
  name: "",
  url: "OrderForm/{file}",
  defaults: new { controller = "OrderForm", action = "Index", file = "" }
);

public class OrderFormController {
  public ActionResult Index(string file) {
    return new EmptyResult();
  }
}

希望一切都有意义。

答案 1 :(得分:1)

编辑问题之后:我的解决方案是,你可以有一个控制器/动作,它应该调用view(cshtml)。您的查询字符串数据应该从viewbag变量传递到视图,并且应该根据viewbag变量调用部分视图。 noo甚至需要编辑路由表(如果你愿意将它作为查询字符串传递)。

//your routeconfig will be
routes.MapRoute(
  name: "default",
  url: "{controller}/{file}",
  defaults: new { controller = "OrderForm", action = "Index", file = "" }
);
//here no need to have 40 routing table one is enough
//your controller/action will be
public class OrderForm
{
  public ActionResult  Index(string file)
  {
    ViewBag.Orderform=file
    return View(file);
  }
    }
//view bag variable is accessible in view as well as in javascript

但我想说,作为最佳实践,您可以修改默认路由以访问所有网址并将其导航到同一控制器/操作,并让该操作返回视图。之后使用angular / knockout js来处理客户端路由并基于它来加载部分视图。(你的网址仍然是40页不同,但是noo需要将它作为查询字符串传递)

//your route table will be
 routes.MapRoute(
      name: "default",
      url: "{controller}/{file}",
      defaults: new { controller = "OrderForm", action = "Index"}
    );

//your controller will be
 public class OrderForm
    {
      public ActionResult  Index()
      {
        return View(file);
      }

导航应由客户端路由

处理