Problems to be aware of when using the MVC Framework (3): How to specify a path for the link

Source: Internet
Author: User

In the view, we will use a lot of a and img labels. They can only accept relative paths. But in the MVC Framework, views are all placed in different folders. How can we better specify their paths?

The general method is to roll back to the upper-level directory through the... method. This can be solved in general views. However, if the Index Action is used, it will be a little troublesome, because the index can be omitted by default. Let's think about it. Do we need to write a... to roll back the level or write two... to roll back the level? This is a very troublesome task.

My practice is to use a variable to save the virtual path of the current website, so that there will be no problem. The following is an example.

 

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Web.Models.PhotoListItem>>" %><% var root = Request.Url.GetLeftPart(UriPartial.Authority);%><% foreach (var item in Model)   {%><a href='<%= root+"/Photo/Large/"+ Html.Encode(item.Path) %>' title="<%= Html.Encode(item.Title) %>">    ' path='<%= root+"/Photo/Normal/"+ Html.Encode(item.Path) %>'        alt="" /></a><% } %>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.