路由所有控制器操作到一个方法,然后根据操作名称显示视图 [英] Route all controller actions to a single method, then display a view according to action name

查看:118
本文介绍了路由所有控制器操作到一个方法,然后根据操作名称显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一堆,在一个通用的方法收集数据,提交表单 - >序列化输入/值对到CSV文件。我想,而无需编写新的控制器动作(这个想法是我不会有编译和部署一个新的装配)添加新的页面提交。所以,这里是我的想法:

Basically I have a bunch of submission forms that gather data in a generic way -> serialize the input / value pairs to a CSV file. I'd like to add new submission pages without having to write a new controller action (the idea being I wouldn't have to compile and deploy a new assembly). So, here's my thinking:

http://foobar.com/Submission/Contact

http://foobar.com/Submission/Apply

这两个路由经过的SubmissionController.Handle()方法。 handle方法,将显示相应的操作视图:

both should route to the SubmissionController.Handle() method. The Handle method will show the view for the appropriate action:

return View("Contact");
// or
return View("Apply");

该意见将都有其形式的行动为/提交/处理和控制器将有:

The views would both have their form's actions as "/Submission/Handle" and the controller would have:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Handle(FormCollection inputs)

因此​​,一个)我怎么都路由的行动联系并申请办理和b)如何从SubmissionController的动作名称查询范围内,所以我可以做的:

So a) how do I route both the actions Contact and Apply to Handle and b) how do I query from within the SubmissionController for the action name so I can do:

var viewName = GetActionName();
return View(viewName);

显然我是一个的n00b到ASP.NET MVC ...因此,如果有更好的方法,请让我知道。

Obviously I'm a n00b to ASP.NET MVC ... so if there's a better method, please let me know.

推荐答案

您必须编写自定义路线。
例如,在global.asax中的默认路由之前:

You would have to write a custom route. For example, in global.asax before the default route:

// Submission/*
routes.MapRoute(
      "Submission",
      "Submission/{method}", 
      new { controller = "Submission", action = "Handle", method = "" }
      );

您可以括号内添加任何东西,那将是关键在RouteValueDictionary路由的价值。需要注意的是控制器和动作的约定使用ASP.NET MVC中。

You can add anything within brackets, and that will be the key to the route's value in the RouteValueDictionary. Note that controller and action are conventions used within ASP.NET MVC.

这就是你拥有它指出:
要访问该在你的控制器称为方法新的密钥,你会做这样的事情:

The way you have it stated: To access this new key called 'method' in your controller, you'd do something like:

string methodName = RouteData.Values["method"];

然后,我想为您的每个方法做一个开关。

Then, I guess do a switch for each of your methods.

也许和更简单的方法:
相反,做一个马桶盖返回一个特定的观点,就是我想要做的是:

Perhaps and easier way: Instead of doing a swtich to return a specific view, what I'd do is:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Handle(FormCollection inputs)
    {
        ViewData["method"] = RouteData.Values["method"];

        return View();
    }

该方法名添加到的ViewData。并返回HandleView。然后,创建一个部分为每个表单类型。在HandleView,放:

add the methodName to ViewData. and return the HandleView. Then, create a partial for each form type. In the HandleView, put:

<% Html.RenderPartial(ViewData["method"]); %>

这样的话,你没有一堆硬codeD路线来处理。但是,你必须处理错误和不正确的方法传递给路线。

That way, you don't have a bunch of hard-coded routes to handle. But, you'll have to handle errors and incorrect methods being passed to the route.

这篇关于路由所有控制器操作到一个方法,然后根据操作名称显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆