【问题标题】:MVC routing path "?" vs "/"MVC 路由路径“?”与“/”
【发布时间】:2015-02-05 08:11:14
【问题描述】:

我有两条返回同一页面的路线。两者都有效。两个页面都有正确的数据 - 除了我的 _layout 页面上的轮播不显示图像。

http://localhost:4556/Controller/Function/41 - 不起作用。

http://localhost:4556/Controller/Function?id=41 - 确实有效。

route.config 文件 ...

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

有人知道这是为什么吗?

_布局

The carousel I am using is such: 
<div id="myCarousel" class="container carousel slide" data-ride="carousel">
            <ol class="carousel-indicators"></ol>
            <div class="carousel-inner" role="listbox"></div>
            <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>

并且脚本中使用的图片路径是:ImagePath: "../Images/Carousel/default-image.png"

未正确显示广告的页面引用了布局页面,例如:

Layout = "~/Views/Shared/_Layout.cshtml";

【问题讨论】:

  • Both pages have the correct data - 这是什么意思?你是说ID被传递给Action吗?我们可以看到_Layout 页面以及究竟是什么“不起作用”吗?
  • ControllerController - 听起来不错)
  • 嗨,是的,对不起。正确的 ID 被传递给控制器​​,我的“非布局”页面中的数据都是正确的并且被正确传递。更多内容......重新布局页面......
  • 不起作用 = 轮播将图像显示为断开的链接。是否:轮播显示中的图像。感谢您的帮助:)

标签: asp.net-mvc controller routing routes


【解决方案1】:

我配置了以下路线:

routes.MapRoute(
    "GetGift",
    "{controller}/{action}/{priceId}",
    new { controller = "Home", action = "GetGift" });

控制器

public class HomeController : Controller
{
    public ActionResult GetGift(int priceId)
    {
        return View();
    }
}

当我导航到这个网址时:http://localhost:20441/Home/GetGift/1 我最终进入了上述操作方法。当我尝试http://localhost:20441/Home/GetGift?priceId=1 时也会发生同样的事情。所以我可以选择使用哪种格式。

配置路由可能是一门黑暗的艺术,但我倾向于尽可能具体地声明它们(在我的示例中,我在路由中特别提到 {priceId} 以避免使用模棱两可的 { id } ),以提供帮助缩小路由引擎必须迭代的选择范围,避免混淆,即路由引擎选择与我预期不同的路由。

【讨论】:

  • 非常感谢。我是否正确理解我应该确保我对控制器的所有调用都是相同的格式?奇怪的是,数据和一切都是正确的,它只是炸弹的轮播。再次感谢你的帮助。欣赏它。
  • 默认路由 ` "{controller}/{action}/{id}` 可能是您网站唯一需要的路由。但是,当您开始在 url 中传递数据时,那就是当您想开始研究使用更多路线时,例如{controller}/{action}/{priceId} 来覆盖可用的网址。
【解决方案2】:

您好,我问了一个和我一起工作的非常聪明的人。

我的图片需要用 @Url.Content(etc..) 引用

感谢所有回答我的人! :)

【讨论】:

    猜你喜欢
    • 2017-06-10
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多