MVC到Web表单重定向而不更改URL

时间:2014-10-31 06:57:16

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我的要求是,将有一个控制器方法,它将处理所有可能返回MVCview / model的请求,或者它可能返回.ASPX页面,无论如何URL必须相同。

类似于以下内容。

 public ActionResult HandleRequest()
    {
       if(Module is Converted)
       {  
          return view(ModuleName);
       }
       else
       {
   //return module.aspx page, Here I can user Redirect method but it will change URL 
    //I don't want browser's Url to be changed.
       }
    }

1 个答案:

答案 0 :(得分:0)

    Your answer is Routing.

    http://msdn.microsoft.com/en-us/library/cc668201.aspx



Look at following code :

 public ActionResult Test()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return RedirectToAction("TestAspx");
        }


        public ActionResult TestAspx()
        {
            ViewBag.Message = "Your app Test aspx page.";

            return View();
        }


Here the action TestAspx() returns an TestAspx.aspx view.

And for the Routing 

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
              "TestAspx",
              "testganesh",
              new { controller = "Home", action = "TestAspx" }
              );


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


        }

Please make appropriate changes to the routing names that you need.

Hope this will help.

Let me know if you still face any issue.

Mark as right if the issue got fixed.
:)