ASP.NET MVC Read files from server - asp.net-mvc

I have 3 questions:
Where is the correct place to put some template files? I'll be using these templates to render emails with DotLiquid. I'm thinking about just having it at ~/Templates/.
How do I unit test this? Should I even unit test reading files from the file system?
Best way to read the file into a string?

I would make a views folder for them /Views/Emails perhaps
Unit test code that you write, not code from the .NET framework imo
string s = System.IO.File.ReadAllText( path );

Check out this blog entry, which talks about how to send emails using a view as a template: ASP.NET MVC 2 Render Template to String.
In short, you create a method that renders the View into a string and then call that method from an action to generate the email body content.

Placing the code in ~/Content/Templates/ and downloading the content using a web client worked best for me.
var welcomeMailTemplatePath = "yourPath";
var webClient = new WebClient();
string html = webClient.DownloadString(WelcomeMailTemplatePath);
This way, you don't need to deal with controllers/views and can directly grab the contents of the template file.

Related

Web page without real files corresponding to URLs?

geniuses!
I need to make a demo page acting like DBpedia (http://dbpedia.org).
Two pages from different URLs,
http://dbpedia.org/page/Barack_Obama and
http://dbpedia.org/page/Lionel_Messi,
show different content.
I cannot really think DBpedia has million pages for all individual entities (E.g., Barack Obama and Lionel Messi).
How can I handle such URL request?
I know a bit about GET request but example URLs above do not seem like to use GET method.
Thank you in advance!
ps. Please teach me the process. Something like:
1. A user enters URL on a browser.
2. ...
When visiting http://dbpedia.org/page/Barack_Obama, your browser does send a GET request, e.g.:
GET /page/Barack_Obama HTTP/1.1
Host: dbpedia.org
The server (dbpedia.org) receives this GET request and then decides what to do. From the outside, you can’t know (for sure) how the server does something. The two common cases are:
Static web page: a file gets served that exists somewhere on the server. The URL path is often mapped to the server’s file system, but that’s not necessarily the case.
Dynamic web page: a file gets served that is generated on the fly. The content often comes from a database, but that’s not necessarily the case.
After trying some solutions, I'm now using Spring Web MVC framework.
Maybe Dynamic web page solution mentioned in unor's answer.
#Controller
public class SimpleDisplayController {
#RequestMapping("/page/{symbolicName:[!-z]+}")
public String displayEntity(HttpServletRequest hsr, Model model) {
String reqPath = (String) hsr.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String entityLb = reqPath.substring(reqPath.lastIndexOf("/"));
model.addAttribute("label", entityLb);
return "entity";
}
}
I could get request using regex as you can see at the 4th line: #RequestMapping("/page/{symbolicName:[!-z]+}").
The function above returns the string 'entity' which is the name of a HTML file serving as a template.
The following code is a body part of the example HTML template.
<body>
<p th:text="'About entity ' + ${label} + '...'" />
</body>
Since I add an attribute with the key 'label' in the controller above, the template can process ${label}.
In the example HTML template, th:text is a snytax of Thymeleaf (Java library to make an XML/XHTML/HTML5 template) which is supported by Spring.

Passing values from C# to VBA code?

I am pretty new to VBA, sorry if this is simple question or already answered... I tried by searching on MSDN but did not get any thing to implement this.
I am in need of sending a string (Host name in URL, e.g. xyzserver:4500/Home/GetExcelData) to VBA code before downloading the Excel to User.
I am calling MVC action method from Excel VBA code, getting some data and displaying in Excel.
My problem is Host name (xyzserver:4500) is different in different servers, I need to update the URL dynamically based on the server URL that the user is accessing from.
Basically i need to send server name to VBA code to update the URL in module. Is there any way to send and maintain values from C# (MVC Action) to VBA?
Session/ Application Cache kind of things are available in VBA?
Thanks in advance
You can pass a parameter from c# to VBA in this manner:
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb = app.Workbooks.Open("your file here which contains the macro");
string url = "blah blah";
app.Run("name of macro here", url);
If you want to edit the vba code programmatically, then you would need to reference the VBIDE. Have a look at Chip Pearson's excellent website: http://www.cpearson.com/Excel/vbe.aspx

How to get current page Url in MVC

I am writing a web app that has to deal with urls containing hash character ("#").
I am using MVC 1 with ASP.NET 3.5 (VS 2008).
My urls are like this one:
www.mysite.com/token/?name1=value1&#&name2=value2
My issue is that I cannot find a method to get the original URL, but only get the substring before the hash character:
www.mysite.com/token/?name1=value1&
I used the MVC methods provided from the class HttpRequestBase.
Anyone can suggest me an alternate method to get the entire url?
Thank you, this is my very first question!
PS: I think maybe I have to encode my hash character, isn'it?
You cannot access anything after the # from the server-side - this is all Client-side. You will need to find another way to pass the information you want through to the server.
If you are posting, you can do this with hidden fields. If you are using ajax posts, you can pass the data within the model.

Designing a service for reading files

Conceptual Overview
Store templated text files on the file system. Read text file into a templating service, get the string, send the email.
I have 2 services so far:
ITemplateService - this is the templating engine (accepts model and the string for templating).
IEmailService - Send email.
Can I have an example of how a file reader service would look, how I can mock or unit test it. Thanks.
I basically want to pass a path/filename to a method and let it read the file and return a string.
System.IO.File.ReadAllText(string filePath) returns the contents.
You may also checkout Postal.

asp.net MVC Read from a http URL

i am trying to use this code:
<%= File.ReadAllText(Server.MapPath("Members/newsletters/welcome.html"))%>
which works great but now the welcome.html file has moved onto another server so i need to read it from an external URL .
any suggestions?
Have a look at the HttpWebRequest class in .NET (there's an example at that URL or have a look at this blog).
try this:
WebClient WebClient = new WebClient();
string YourContent = WebClient.DownloadString(YourUrl);
You need to be more specific. Are you trying to read it from an UNC path or over the web? Is the other on your network?
If UNC path, probably easiest to map a drive on your local server so it looks like a local file.
Of course, I'm wondering why you're doing it this way at all. Why not replicate the content to all web servers? Reading over network is expensive. Also, that logic should probably be encapsulated in a helper method which can handle caching. You might consider moving the logic to retrieve that content to your controller. A view should render the model data given to it.

Resources