Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

visual studio 2010 - How to reference .html page from .aspx page in the same VS2010 solution?

This question has to do with the MVC framework and file arrangement conventions within the MVC project folders in VS 2010. It also has to do with filenamepath conventions for project files (referencing them from *.aspx and *.html).

To assist in answering the question I created a sample project titled "PAPlus.Asp" (a made-up name). Steps to reproduce: open VS2010SP1Rel->File->NewProject->ASP.Net MVC 2 Empty Web Application->Name = "PAPlus.Asp"

My question is this: if I create a MVC 2.0 application (example below) and want to link a *.html file that is in my project folder located at ..PAPlus.AspViewsHomeShowCuteDog.html how do I a href the html file from within index.aspx?

I am using the following DataProvider:

  <connectionStrings>

    <add name="PaPlus"
         connectionString="Data Source=|DataDirectory|PAPlus.sdf"
         providerName="System.Data.SqlServerCe.4.0"/>

  </connectionStrings>

I created the following:

namespace PAPlus.Asp.Models
{
    public class User
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
    }
}

namespace PAPlus.Asp.Models
{
    public class PaPlus : DbContext
    {
        public DbSet<User> Users { get; set; }
    }
}

namespace PAPlus.Asp.Controllers
{
    public class HomeController : Controller
    {
        PaPlus _paPlus = new PaPlus();

        public ActionResult Index()
        {
            var users = from u in _paPlus.Users
                        select u;

            return View(users);
        }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(User newUser)
    {
        if (ModelState.IsValid)
        {
            if (_paPlus.Users.Any(d => d.UserName == newUser.UserName))
            {
                ModelState.AddModelError("UserName", "The username is not unique");
                return View(newUser);
            }

            _paPlus.Users.Add(newUser);
            _paPlus.SaveChanges();

            return RedirectToAction("Index");
        }

        return View(newUser);

    }

}
}

I also created VIEWS but they too long to post here. I will continue my explanation showing the body of index.aspx:

<body>
    <div>   
        <h1>Application Users</h1>
    <ul>    
    <%
        foreach (User d in Model)
        {%>
        <li> 
            <%=d.UserName%> (<%=d.UserId%>)
        </li>
    <%
        }%>
    </ul>
    <p>
        <%=Html.ActionLink("Create New User", "Create")%>
    </p>
    <p>
        <a href="Views/Home/ShowCuteDog.html">Navigate to Cute Dog Page!</a>
    </p>
    </div>
</body>

With all of the above explained, I can target a specific line that I would like assistance with:

<a href="Views/Home/ShowCuteDog.html">Navigate to Cute Dog Page!</a>

Obviously the path to ShowCuteDog.html is invalid. However, it seems no matter what I do I cannot enter a valid path.

I would like to know how to open ShowCuteDog.html using the a href

I would also like to know where I should store ShowCuteDog.html within my MVC project solution folder structure (/Views/Home/ seems like the best place, perhaps I am wrong?)

Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not put content under Views that will be directly linked. Browser access to that path is blocked in Views/web.config by default, and for very good reasons: you don't want users directly loading files from there.

So you should put plain html somewhere else; perhaps in /html instead? Then link it as <a href="/html/cutepuppies.html">


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...