Sitecore internal links to media files?

2019-09-07 06:26发布

问题:

In a Sitecore 6.5 solution, i have the following;

<sc:Text runat="server" Field="bodyText"/>

and

Sitecore.Context.Item["bodyText"]

I use the latter many places in the solution - the first not so much. I have now discovered that when using the latter, all links to the medialibrary is in the format ~/media/31F0E8084F9443789F25836A2B285D3E.ashx even though Media.UseItemPaths is set to true in the web.config.

However, when i use the first mentioned method <sc:Text /> the links to medialibrary items are in the correct format (without the guid, but the folder structure of the media library).

Whats going on?

回答1:

As halbherz mentioned the latter

Sitecore.Context.Item["bodyText"]

is the literal value of the field. It doesn't go through the sitecore RenderField pipeline so none of the links get expanded.

If you want to render a field from the code behind you can use the FieldRenderer class like:

Sitecore.Web.UI.WebContols.FieldRenderer.Render("bodyText", Sitecore.Context.Item)


回答2:

Just to add further clarification (what @halbherz and @marto say is totally correct) just so you can better understand ~why~ they are like this.

Within RichText fields links to media and other sitecore items are stored in this format (~/media/31F0E8084F9443789F25836A2B285D3E.ashx) using the 'short GUID' of the item so that if someone renames or moves your media / content item then the link will still be valid (rather than embed the full name & path to the item in the field).

When you use the FieldRenderer (or controls that inherit from FieldRenderer such as sc:text / sc:image etc) the RenderField pipeline is called and the links are processed by the LinkManager (see your web.config <LinkManager> section for more info on config). The LinkManager exists so that all links are processed in a consistent manner. Calling the field value directly bypasses this pipeline and you get the raw text of the field.

You can create your own links similar to this using : LinkManager.GetDynamicUrl(item);



回答3:

The first one is a control which goes through certain pipelines and redners saved item references as friendly urls.

The second one is a direct value call which returns the value unedited. To get a friednly url you should use the control.

Why do you need to call the value directly?