重定向到另一个控制器+ ASP.Net MVC3 中不更改 URL 的操作

发布于 2024-11-19 10:26:22 字数 1924 浏览 7 评论 0原文

注意:下面只是一个小演示,用于模拟我正在寻找的内容:

下面是我的应用程序上的网址格式,用户可以看到

mydomain.com/cat/1  --display cat with id 1 |controller=Cat, action=DisplayDetails
mydomain.com/dog/2  --display dog with id 2 |controller=Dog, action=DisplayDetails
mydomain.com/cow/2 --display cow with id 3  |controller=Cow, action=DisplayDetails

我维护了一个系统,其中没有 2 只动物(可能是不同的) kind) 可以有相同的 id,这意味着如果有一只 id=1 的猫,我们不能有任何其他具有该 id 的动物。另外,从我的系统中,我可以从动物 id 中提取动物详细信息+类型

除了现有的 URL 模式之外,我计划创建一个格式如下的短 URL

mydomain.com/1  --this will show cat
mydomain.com/2  --this will show dog
mydomain.com/3  --this will show cow

我创建的路由如下,它们在 global.asax 中出现相同的顺序

pattern= Cat/{id}, controller= Cat, action=DisplayDetails
pattern= Dog/{id}, controller= Dog, action=DisplayDetails
pattern= Cow/{id}, controller= Cow, action=DisplayDetails
pattern= {id}, controller= DisplayAnyAnimal ----------i want help in this Route

< strong>当前控制器看起来像这样

public class DisplayAnyAnimalContoller : Controller
{
      public ActionResult Index(string animalId)
      {
           //iam processing request here from animalId
           //now i know which contoller+action needs to be executed

          //say for instant i have to display dog with id=2

          //currently iam doing this to redirect and its working fine, 
          //but it changes url
          -----------------------------------------------
          #########################
          ### i need help here  ###       
          #########################
         return RedirectToRoute(new {contoller="Dog",action="DisplayDetails",id=2 });             
          -----------------------------------------------
      }
}

现在,RedirectToRoute / RedirectToAction 的问题是它们都更改了 URL。但我不想改变我的网址模式。

请建议我如何实现这一目标,您可能会建议一些完全不同的方式来实现这一目标

Note: Below is just a small demo sort to simulate what i am looking for:

Below are the urls format on my app that user can see

mydomain.com/cat/1  --display cat with id 1 |controller=Cat, action=DisplayDetails
mydomain.com/dog/2  --display dog with id 2 |controller=Dog, action=DisplayDetails
mydomain.com/cow/2 --display cow with id 3  |controller=Cow, action=DisplayDetails

I have maintained a system where no 2 animals(may be of different kind) can have same id, it means if there is a cat with id=1, we cant have any other animal with that id. Also from my system i can extract animal details+ type just from animal id

Apart from existing URL pattern, I am planning to create a short Url in format as below

mydomain.com/1  --this will show cat
mydomain.com/2  --this will show dog
mydomain.com/3  --this will show cow

Routes i have created are as below, and they appear same order in global.asax

pattern= Cat/{id}, controller= Cat, action=DisplayDetails
pattern= Dog/{id}, controller= Dog, action=DisplayDetails
pattern= Cow/{id}, controller= Cow, action=DisplayDetails
pattern= {id}, controller= DisplayAnyAnimal ----------i want help in this Route

Currently Controller looks like this

public class DisplayAnyAnimalContoller : Controller
{
      public ActionResult Index(string animalId)
      {
           //iam processing request here from animalId
           //now i know which contoller+action needs to be executed

          //say for instant i have to display dog with id=2

          //currently iam doing this to redirect and its working fine, 
          //but it changes url
          -----------------------------------------------
          #########################
          ### i need help here  ###       
          #########################
         return RedirectToRoute(new {contoller="Dog",action="DisplayDetails",id=2 });             
          -----------------------------------------------
      }
}

Now the problem with RedirectToRoute / RedirectToAction is they both changes the URL. But i dont want to change my url pattern.

Please suggest me how to achieve this, you may suggest some entirely different way, to achieve this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-11-26 10:26:22

您可以编写自定义动物路线:

public class AnimalRoute : Route
{
    public AnimalRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    { }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        var id = rd.GetRequiredString("id");

        // TODO: Given the id decide which controller to choose:
        // you could query the database or whatever it is needed.
        // Basically that will be the code you had in the beginning
        // of the index action of your DisplayAnyAnimalContoller which
        // is no longer needed.
        if (id == "1")
        {
            rd.Values["controller"] = "Cat";
        }
        else if (id == "2")
        {
            rd.Values["controller"] = "Dog";
        }
        else if (id == "3")
        {
            rd.Values["controller"] = "Cow";
        }
        else
        {
            // no idea what this animal was
            throw new HttpException(404, "Not found");
        }
        rd.Values["action"] = "index";
        return rd;
    }
}

然后在 Global.asax 中注册它:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.Add(new AnimalRoute("{id}", new MvcRouteHandler()));
}

现在,当您导航到 mydomain.com/1 时,GetRouteData > 自定义路由的方法将被执行,将获取 id=1,然后它将使用 Cat 控制器的 Index 操作。

You could write a custom animal route:

public class AnimalRoute : Route
{
    public AnimalRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    { }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        var id = rd.GetRequiredString("id");

        // TODO: Given the id decide which controller to choose:
        // you could query the database or whatever it is needed.
        // Basically that will be the code you had in the beginning
        // of the index action of your DisplayAnyAnimalContoller which
        // is no longer needed.
        if (id == "1")
        {
            rd.Values["controller"] = "Cat";
        }
        else if (id == "2")
        {
            rd.Values["controller"] = "Dog";
        }
        else if (id == "3")
        {
            rd.Values["controller"] = "Cow";
        }
        else
        {
            // no idea what this animal was
            throw new HttpException(404, "Not found");
        }
        rd.Values["action"] = "index";
        return rd;
    }
}

and then register it in Global.asax:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.Add(new AnimalRoute("{id}", new MvcRouteHandler()));
}

Now when you navigate to mydomain.com/1, the GetRouteData method of the custom route will be executed, will fetch the id=1, and then it will use the Index action of the Cat controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文