ASP MVC重定向而不更改URL(路由) [英] ASP MVC Redirect without changing URL(routing)

查看:848
本文介绍了ASP MVC重定向而不更改URL(路由)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个小问题,完成这个IM,希望有人能帮助我。目前使用C#。

im having a little problem accomplishing this, hopefully someone can help me out. Currently using c#.

目标:
我希望能够输入网址:
 www.mysite.com/NewYork
要么
 www.mysite.com/name-of-business

Goal: I want to be able to type URL: www.mysite.com/NewYork OR www.mysite.com/name-of-business

根据字符串我想路由到不同的操作不改变网址。

Depending on the string I want to route to different actions without changing the URL.

到目前为止,我有:
在:
公共静态无效的RegisterRoutes(RouteCollection路线)

So far i have: In: public static void RegisterRoutes(RouteCollection routes)

        routes.MapRoute(
         "UrlRouter", // Route name 
         "{query}", // URL with parameters 
         new { controller = "Routing", action = "TestRouting" } // Parameter defaults
         );

在控制器我有:

    public ActionResult TestRouting(string query)
    {
        if (query == "NewYork")
            return RedirectToAction("Index", "Availability");   <--------- not sure
        else if (query == "name-of-business")
            return Redirect("nameofbusines.aspx?id=2731");       <--------- not sure
        else
            return RedirectToAction("TestTabs", "Test");         <-------- not sure

    }

我有pretty多竭尽所能来重定向/转移到页面,而不
更改URL,但一切我试过改变URL或给我一个错误。

I have pretty much tried everything to redirect/transfer to the page without changing the URL, but everything i've tried changes the url or gives me an error.

基本上即时寻找的Server.Transfer相当于在那里我可以保持网址
但发送信息的行为,并把它显示它的结果。

Basically im looking for the equivalent of server.transfer where i can keep the url but send info to the action and have it display its result.

任何投入将是AP preciated。

Any input would be appreciated.

推荐答案

我和尼克是在这一个,但我认为你可以只使用,而不必做定期的谐音意见。您可能需要实现它们的共享视图,如果他们没有在相应的控制器(因为它只会在关联和共享视图看)的意见。

I'm with Nick on this one, though I think you could just use regular views instead of having to do partials. You may need to implement them as shared views if they are not in the views corresponding to the controller (since it will only look in the associated and shared views).

public ActionResult TestRouting(string query)
{
    if (query == "NewYork")
    {
        var model = ...somehow get "New York" model
        return View("Index", model );
    }
    else if (query == "name-of-business")
    {
        var model = ...get "nameofbusiness" model
        return View("Details", model );
    }
    else
    {
        return View("TestTabs");
    }
}

每个视图会再取模型的特定实例,并使用模型渲染它的内容。该网址不会改变。

Each view would then take a particular instance of the model and render it's contents using the model. The URL will not change.

您使用RedirectResult任何时候,你实际上将发送一个HTTP重定向到浏览器,这将迫使一个URL的变化。

Anytime that you use a RedirectResult, you will actually be sending an HTTP redirect to the browser and that will force a URL change.

这篇关于ASP MVC重定向而不更改URL(路由)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆