2

在我看来,我正在使用 ASP.NET MVC 3 和帮助程序 @Html.Raw。

我正在向它传递一些我存储在数据库中的 HTML 标记。该标记包含一些指向我网站上其他位置的 URL。例如http://www.foo.fom/events。这些数据是论坛帖子,因此显示它们的页面的格式为http://www.foo.com/forums/thread/42/slug

但是,在呈现页面时,保存的 URL 会以修改后的形式呈现为:

http://www.foo.com/forums/thread/42/events/

这只发生在我网站上的 URL 上。如果 URL 指向某个外部站点,则它保持不变。

我已经验证我传递给@Html.Raw 的是正确的URL (http://www.foo.com/events)。为什么在呈现页面时它会发生变化?有没有一种简单的方法可以禁用此“功能”?

这是我显示标记的代码:

<div>
        @Html.Raw(post.Body)
</div>

这是生成页面数据的控制器代码:

var post = _forumRepository.GetPostById(id)

var model = new ForumPostView()
{
    Body = post.Body,
    PostDate = post.DatePosted,
    PostedBy = post.Author,
    PostId = post.Id
};

return View(model);

我已经通过调试器验证了 post.Body 中的确切 URL 在被传递回视图之前的格式为“http://www.foo.com/events”(没有尾部斜杠)。我还通过调试器验证了该值在传递到@Html.Raw 之前没有改变。

4

2 回答 2

1

It sounds like the urls that are pointing to other pages on your site are non-absolute. Are you certain they start with a / or http? If not, it's behaving exactly as it's supposed and treating them like relative urls -- and thus appending them to the current url.

(Html.Raw will not manipulate the string, so it's not at fault here)

Also, it wouldn't hurt to show us your code.

于 2012-04-06T18:42:15.700 回答
1

No, in fact I am an idiot. The URLs were indeed stored in relative form without a leading /, which is why they ended up being relative to the current page. The text displayed was absolute, which is what I saw when I looked at the db. That's what I get for debugging on a few hours' sleep ;)

于 2012-04-06T23:35:30.100 回答