【问题标题】:MVC Reroute to accept KeyMVC 重新路由以接受密钥
【发布时间】:2023-03-03 01:59:01
【问题描述】:

从事 MVC 项目,我在重新路由我的 URL 时遇到了困难。 我目前拥有的是

http://dev.mywebsite.com/s/index?Key=abc123

然后运行索引操作并按我的意愿完成 我希望能够输入

http://dev.mywebsite.com/s/abc123

然后像往常一样运行索引操作

我现在有

        routes.MapRoute(null, "s/index/{id}", new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional
        }
             );

但我有点不知道从这里去哪里。任何帮助将不胜感激。 谢谢

编辑:我的完整 routeconfig 类

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("sites/{*pathInfo}");

        routes.MapRoute(null, "s/index/{key}", new
        {
            controller = "S",
            action = "Index",
            key = UrlParameter.Optional
        }
        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Site", action = "Index", id = UrlParameter.Optional },
            new[] { "Custom.Web.Controllers" }
        );
    }

在我的控制器中,我的 actionresult 索引为

    public ActionResult Index(string Key)
    {
        return Redirect(workflow.RetrieveURL(Key));
    }

【问题讨论】:

  • 你能不能试试"s/{key}"的模式,同时把id = UrlParameter.Optional改成key = UrlParameter.Optional
  • @AndreCalil 不幸的是,我仍然收到服务器错误,我还想补充一点,我希望 s/MyOtherAction 正常运行,但我只想在我转到 dev.mywebsite.com/s/abc123 时以 abc123 为键运行索引操作
  • 能否提供错误详情?此外,最好有Home/Index 动作的签名。您还有其他路由规则吗?
  • @AndreCalil 我已按要求添加了额外的代码。请看看,让我知道你的想法。我不断收到 404 错误。 '找不到页面'
  • 是的,你的模式应该是"s/{key}"而不是"s/index/{key}"

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


【解决方案1】:

所以,在我们所有的cmets之后,解决方案是:

routes.MapRoute(null, "s/{Key}",
new {
  controller = "Home",
  action = "Index",
  Key = UrlParameter.Optional
});

将此规则放在所有其他规则之前以给予优先权

【讨论】:

  • @user2094139 很高兴为您提供帮助 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
相关资源
最近更新 更多