【问题标题】:MVC and Asp.net hybrid Application starting pointMVC 和 Asp.net 混合应用起点
【发布时间】:2011-04-19 13:09:51
【问题描述】:

我有一个 MVC 和 Asp.net 混合应用程序我有一个控制器,但它没有任何主控制器,我不想从 mvc 视图启动应用程序我需要从 default.aspx 启动

当我在 url 末尾写“default.aspx”时,它工作正常。但是当我写 mysite.com/ 时它不起作用

我也有一个网络服务,但它现在不工作。因为路由喜欢:“mywebservice.asmx/mymethodname”

如何设置我的 global.asax ?在这种组合中运行我需要什么配置?

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 asp.net-4.0


    【解决方案1】:

    您可以在 Web 应用程序的子文件夹中创建 MVC 应用程序,然后将任何 Global.asax 配置移动到您的 WebForms 应用程序

    您只需要更新您的路由,如果您的 MVC 应用程序是在名为 MVC 的文件夹中创建的,它可能看起来像这样:

               routes.MapRoute(
               "MVC default",                                              
               "MVC/default.aspx",                                         
                new { controller = "Home", action = "Index", id = "" }  
              );
    
            routes.MapRoute(
               "Services",
               "MVC/{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = "" }
                );
    

    编辑:

    实际上,您需要映射以确保您的视图也正确映射,您可以通过创建快速自定义视图引擎来做到这一点:

            public class CustomViewEngine : System.Web.Mvc.WebFormViewEngine
    {
        public CustomViewEngine()
        {
            base.ViewLocationFormats = new string[]
            {
              "~/MVC/Views/{1}/{0}.aspx",
              "~/MVC/Views/{1}/{0}.ascx",
              "~/MVC/Views/Shared/{0}.aspx",
              "~/MVC/Views/Shared/{0}.ascx"
            };
    
               base.PartialViewLocationFormats = new string[] {
             "~/MVC/Views/{1}/{0}.aspx",
              "~/MVC/Views/{1}/{0}.ascx",
              "~/MVC/Views/Shared/{0}.aspx",
              "~/MVC/Views/Shared/{0}.ascx"
            };
    
               base.MasterLocationFormats = new string[]
            {
              "~/MVC/Views/{1}/{0}.master",
              "~/MVC/Views/Shared/{0}.master"
            };
        }
    
    }
    

    然后在你的 global.asax 中注册它:

            System.Web.Mvc.ViewEngines.Engines.Clear();
            System.Web.Mvc.ViewEngines.Engines.Add(new CustomViewEngine());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      相关资源
      最近更新 更多