是否可以在运行时动态指定操作名称?

发布于 2024-11-15 17:53:02 字数 221 浏览 3 评论 0原文

string actionName = "Users";

[HttpGet]
[ActionName(actionName)]
public ActionResult GetMe()
{

}

...给出:非静态字段、方法或属性需要对象引用

这只是一个测试,有没有办法做到这一点?如果是这样,我可以重复使用相同的控制器,并可能动态创建新的 URI...对吧?

string actionName = "Users";

[HttpGet]
[ActionName(actionName)]
public ActionResult GetMe()
{

}

...gives: An object reference is required for the non-static field, method, or property

That was just a test though, is there a way to do this? If so, I could re-use the same Controller and possibly create new URIs on the fly... right?

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

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

发布评论

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

评论(2

暮倦 2024-11-22 17:53:02

假设您有以下控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

您可以编写一个自定义路由处理程序:

public class MyRouteHander : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var rd = requestContext.RouteData;
        var action = rd.GetRequiredString("action");
        var controller = rd.GetRequiredString("controller");
        if (string.Equals(action, "users", StringComparison.OrdinalIgnoreCase) &&
            string.Equals(controller, "home", StringComparison.OrdinalIgnoreCase))
        {
            // The action name is dynamic
            string actionName = "Index";
            requestContext.RouteData.Values["action"] = actionName;
        }
        return new MvcHandler(requestContext);
    }
}

最后在您的路由定义中关联自定义路由处理程序:

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

现在,如果您请求 /home/users ,它就是 Index 将提供服务的 Home 控制器的操作。

Assuming you have the following controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

you could write a custom route handler:

public class MyRouteHander : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var rd = requestContext.RouteData;
        var action = rd.GetRequiredString("action");
        var controller = rd.GetRequiredString("controller");
        if (string.Equals(action, "users", StringComparison.OrdinalIgnoreCase) &&
            string.Equals(controller, "home", StringComparison.OrdinalIgnoreCase))
        {
            // The action name is dynamic
            string actionName = "Index";
            requestContext.RouteData.Values["action"] = actionName;
        }
        return new MvcHandler(requestContext);
    }
}

Finally associate the custom route handler in your route definitions:

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

Now if you request /home/users it's the Index action of the Home controller that will be served.

何以笙箫默 2024-11-22 17:53:02

您可以只接受另一个路由参数并执行 switch 语句。

You can just take another routing argument in and do a switch statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文