In my .net mvc application, I need to dynamically insert contents from local files into a View page. So that the contents from different files can be display with the same style. So I created a controller called StaticController and an action called General. Following is my code:
public class StaticController : Controller
{
public virtual ViewResult General(string filePath)
{
return View((object)filePath);
}
}
In my view I try to display the contents of the file with the filePath.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Tab.Master" Inherits="System.Web.Mvc.ViewPage<string>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%if(string.IsNullOrEmpty(Model)){ %>
Sorry, page is under construction!
<% return;} %>
<%=Url.Content(Model)%>
</asp:Content>
Then I tested the view with http://localhost:4789/Static/General?filePath="~/staticfiles/1.txt" and then I expect the content of 1.txt to be displayed. But all I got back is a nice "~/staticfiles/1.txt" on my screen.
Did I do something wrong? I used to display pictures this way. I guess with plain text, Url.Content doesn't work any more?