Ordinary html links in mvc razor

2019-07-09 04:00发布

问题:

I have an mvc razor app that's humming along nicely, and I just tried to drop in a static html Terms and Conditions page. When I try to link to it from my opt-in cshtml with

<a href="~/Views/UserPromotion/TechFundTnC.html">Terms and Conditions</a>

It 404s at runtime with "resource cannot be found".

It's in the compiled folder. The path and name are correct (I picked it with intellisense, so it knows it's there). The link cshtml page and destination html page are even in the same folder (I've tried using just the filename too).

It's just plain html. It shouldn't need any fiddling with routing or anything. Why can't it find it?

回答1:

~/Views is inaccessible by default and ~ doesn't work in static HTML, you can use this and it should get you the relative path and work

Put your static HTML file in ~/Content/UserPromotion and use

<a href="@Url.Content("~/Content/UserPromotion/TechFundTnC.html")">Terms and Conditions</a>