混合asp.net和MVC Web应用程序

时间:2015-08-10 15:32:25

标签: asp.net asp.net-mvc

我有一个在asp.net webform应用程序中运行的网站。目前,我们计划将此webform应用程序转换为具有以下条件的MVC。

  1. 仅以MVC模式开发新模块。
  2. 现有模块将保留在aspx页面中。 (没有转换为MVC)。
  3. 主页将是(default.aspx)
  4. 例如www.example.com将指向www.example.com/default.aspx

    将开发的新模块将具有以下参数。

     www.example.com/place/Japan
     www.example.com/place/USA
     www.example.com/place/(anycountry)
    

    所以我开发了一个名为place的控制器。

      public class PlaceController : Controller
      {
        //
        // GET: /Place/
          [HttpGet]
        public ActionResult Index()
        {
    
            return View();
        }
    
       public ActionResult Index(string country)
          {
    
              return View();
          }
    

    当我输入以下网址时: http://example.com/转到http://example.com/default.aspx

    我的routeconfig有:

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

    但是当我添加以下内容时

      routes.MapRoute(
              name: "PlaceController",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Place", action = "Index", id = UrlParameter.Optional }
          );
    

    并输入www.example.com它会转到控制器,而不是转到default.aspx页面。我将如何解决这个问题

    如果我输入www.example.com/place/japan,我会找到资源未找到。我该如何修改索引actionresult?

1 个答案:

答案 0 :(得分:1)

你不能有两个名为name方式的行为(" Index")并且都会响应GET请求,因为MVC不知道要使用哪个(编辑,除非您使用[HttpGet]装饰一个。这将优先于其他

因此,在您的Controller中,您需要使用" country"重命名该控制器。参数到" IndexByCountry"。然后试试这个:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        name: "Place",
        url: "Place/",
        defaults: new { controller = "Place", action = "Index" }
        );

    routes.MapRoute(
        name: "PlaceByCountry",
        url: "Place/{country}",
        defaults: new { controller = "Place", action = "IndexByCountry", country = "" }
        );
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { id = UrlParameter.Optional });
}