MVC和Asp.net混合应用程序的起点

时间:2011-04-19 13:09:51

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

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

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

时它不起作用

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

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

1 个答案:

答案 0 :(得分:0)

您可以在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());