【问题标题】:MVC to web form redirection without changing URLMVC 到 Web 表单重定向而不更改 URL
【发布时间】:2014-10-31 06:57:16
【问题描述】:

我的要求是,将有一个控制器方法来处理所有请求,它可能返回 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.
       }
    }

【问题讨论】:

  • 只是为了澄清问题,您希望通过一个 URL 字符串加载多个视图之一。加载的视图基于 URL 字符串中未包含的逻辑...此后您希望加载页面并仍然保留现有字符串。这是你想要达到的目标吗?
  • @AbdulG,是的,你明白了,假设... www.mysite.com/Controller/Action 链接应该能够加载任何视图或 webform.aspx 页面而不更改其 URL .
  • 你可以通过局部视图来实现这一点。使用模板视图(可能只是将其命名为HandleRequest,以便可以使用您现有的操作返回)。从那里,您可以根据您计划使用的任何逻辑在模板中渲染部分视图
  • 好吧,返回任何视图不是我关心的问题,我关心的是在某些情况下我需要在同一个 URL 上呈现 .aspx 页面。

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


【解决方案1】:
    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.
:)

【讨论】:

  • 能否请您多介绍一下,因为这个aspx页面是物理文件,所以我认为它不会起作用,并且不能更改URL。
  • 我认为路由不会实现@DotNetIsMyPower 的要求
猜你喜欢
  • 1970-01-01
  • 2011-01-02
  • 2011-07-04
  • 2013-05-31
  • 2015-04-02
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多