【问题标题】:How to handle actions and views names in my ASP.NET MVC application?如何处理我的 ASP.NET MVC 应用程序中的操作和视图名称?
【发布时间】:2011-01-18 08:09:20
【问题描述】:

我有一个 ASP.NET MVC 应用程序,我不想像这样显式编写操作或视图名称:

return RedirectToAction("索引");要么 返回视图(“主页”);

处理这些字符串的最佳做法是什么?

我使用 Visual Studio 2010 和 ASP.NET MVC2

【问题讨论】:

    标签: asp.net-mvc-2 string embedded-resource


    【解决方案1】:

    MVCContrib,里面有扩展方法允许你写:

    return RedirectToAction<HomeController>(x => x.About());
    

    另一种可能是T4 templates

    【讨论】:

    【解决方案2】:

    使用T4MVC。它将允许您杀死代码中的所有魔术字符串。

    【讨论】:

      【解决方案3】:

      我已经创建了一个像这样的静态类:

      public static class Routing {
        public static RouteValueDictionary Index {
          get {
            return new RouteValueDictionary {
              { "controller", "Default" },
              { "action", "Index" }
            };
          }
        }
      }
      

      所以我可以这样使用它:

      return RedirectToAction(Routing.Index);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-27
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-19
        相关资源
        最近更新 更多