asp.net MVC Read from a http URL - asp.net-mvc

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.

Related

Get SPList and SharePoint.Client.List web relative url

I am working with sharepoint list and sharepoint client list objects. Is there any way to get web relative urls of those objects?
I know about ServerRelativeUrl, DefaultViewUrl but they don't solve my problem.
I want exactly web relative urls, without view url parts.
For example my tasks list has a url like this
http://example.com/sites/developer/lists/tasks/CustomDefaultView.aspx
and i want only lists/tasks part.
For example my shared documents has a url like this
http://example.com/sites/developer/shared%20documents/Forms/AllItems.aspx
and i want only shared%20documents part.
I need this both for SPList class and for SharePoint.Client.List class
thanks!
So i found the solution. I need to use SPList.RootFolder.Url which returns Web site-relative url of the list(just what i want), and also need to do some manipulations for Client.List class as has no RootFolder.Url property, instead it has RootFolder.ServerRelativeUrl. So if i subtract the server relative url of the parent web of that list from RootFolder.ServerRelativeUrl i will get what i wanted for Client.List class.

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

Get request URI in jinja2?

I'm working on a Pylons project using Jinja2 templating. I want to test for the request URI and/or controller inside the Jinja2 templates - is there an equivalent to a getRequestUri() call? I can set a context variable as a flag inside all the controller methods to do what I want, but that seems a bit like writing my home address on each and every one of my house keys... i.e. not quite the right way to do it.
Solution: not quite a function call, but I can test against url.environ.PATH_INFO. It only gives me the URL path, not the hostname, and I don't know that it would give me the query string, but it gives me what I need.

ASP.NET MVC Read files from server

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.

Correct practices for where to put images and static files in an ASP.NET MVC application?

There is a directory in the standard ASP.NET template "Content" where most people seem to be putting in images and css files etc.
For instance stackoverflow's logo:
(source: stackoverflow.com)
actually is refered to with a server path containing 'content' in the URL (just do View Source for any SO page and you'll see this). So they obviously are storing images in "content/images/...".
src="/Content/Img/stackoverflow-logo-250.png"
Edit: Sometime in the last 10 years they changed the path - but this is what it used to be.
I dont particularly like this. My HTML ends up with /content all over it, and its slightly harder to migrate existing pages that just have /image. Fortunately my stylesheet doesnt end up with content all over it, as long as I save it in content\site.css.
An alternative is to put an images directory in the root, but then you get images at the same level as Controllers and Views which is pretty horrible.
I'd wondered about trying to add a redirection rule like this :
routes.RedirectRoute(
"images rule",
"Images/{*}",
"Content/Images/{1}"); // this is NOT valid code - I made it up
But that code doesnt work because I just made it up. I could use a third party redirection/rewriting plug-in but I want to keep everything 'pure' within the MVC model.
What has anyone else found in this area? Or is everyone just happy with an extra ("/content".length) bytes in their source for every image they serve.
To be honest, I don't think its really something to worry about... "/Content" is going to make a pretty minimal contribution to your page size. If you still want to do it, here are some options:
Option 1, if you are running on your own server, is to check out the IIS URL Rewrite module: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/
Option 2 is to use either RedirectResult, or ContentResult, to achieve the same effect in the MVC framework
First, map a "catchall" route under "Images/" to a controller action, like so
routes.MapRoute("ImageContent",
"Images/{*relativePath}",
new { controller = "Content", action = "Image" })
Make sure it is above your standard "{controller}/{action}/{id}" route. Then, in ContentController (or wherever you decide to put it):
public ActionResult Image() {
string imagePath = RouteData.Values["relativePath"]
// imagePath now contains the relative path to the image!
// i.e. http://mysite.com/Images/Foo/Bar/Baz.png => imagePath = "Foo/Bar/Baz.png"
// Either Redirect, or load the file from Content/Images/{imagePath} and transmit it
}
I did a quick test, and it seemed to work. Let me know in the comments if you have any problems!
It's usually better to put images under a different sub domain. The reason for this is browsers limit the number of connections per URL. So if you use http://static.mysiste.com now the browser can open more concurrent connections due to it being in a different URL.

Resources