Advice for displaying image in MVC using inline style - model-view-controller

I am new to MVC, so please forgive me! I have the following line in my
Views/Home/Index.cshtml file:
<div class="grid-content" style="background-image: url('~/Content/pics/mypic.jpg');">
(The _Layout.cshtml renders it using the standard RenderBody() call.)
But it will not display the pic. If I replace the path with a full qualified path to an internet pic, it will work. It won't work with the above syntax.
It seems to me that it has to do with the multiple uses of single/double quotes, maybe.
Is there a way for this to work? I have yet to learn how to make use of Url.Content, or some other helper method.
Any suggestions?

The url() you pass is a path to the resource from your web server, either relative to the calling page or an absolute URL.
The example you have provided looks like a file-system path, not a URL that your web server understands, which is why fully-qualified URLs to an "internet image" work.
Your document is stored in Views/Home/Index.cshtml. If the image is stored in the same folder , then simply change it to url('mypic.jpg'). If not, adjust accordingly.
Change the path from ~/Content/pics/mypic.jpg to something that is relative to the document you are serving. For example, if the HTML
(Yes, URLs can contain tildes, but generally they designate "what follows is a system username", like /~Coreysan/Content/pics/mypic.jpg, which would direct your web server to find your ~/<whatever your web server folder is>/Content/... resource).

Related

How to build WOPI validation application

http://wopi.readthedocs.io/en/latest/build_test_ship/validator.html
As per this documentation, we need to build some HTML host page.
In this document, they mention one point that I didn't understand completely
The simplest way to use the validation application is to use the view
action. To use the view action hosts should treat .wopitest files the
same way other Office documents are treated. In other words, hosts
should do the following:
Launch a host page pointed at the .wopitest file. Ideally, this should
be the same host page used to host regular Office Online sessions.
This will allow the validation application to test things like
PostMessage and do some validation on the way the Office Online iframe
was loaded.
What is .wopitest file and what I need to do exactly, Can anyone please guide me ? please explain stepwise
They mean that you have to call that action in the same way you call any other action (looking for the action url in the discovery, etc.).
For example, when you want to open a .docx for view, you look into the discovery.xml and you find that you have to call https://word-view.officeapps-df.live.com/wv/wordviewerframe.aspx?", and of course you must have ready the Rest services for that action (checkFileInfo, etc.) and the WOPISrc pointing to a file that in fact is a .docx . Well, for the validation application is exactly the same, but you should call to WopiTest app instead of Word app (inside the discovery) and your generated WOPISrc should point to a file called "xxxx.wopitest" instead "xxxx.docx". That's it! ;)

GAE Image service change root path

I would like to know if it is possible to change dynamic image service root path instead of accessing image like this http://your_app_id.appspot.com/randomStringImageId i would like to access it by http://your_app_id.appspot.com/i/randomStringImageId.
I believe that's just an example. Actual urls created by get_serving_url() run off of one of google's domains, they tend to look more like:
http://lh1.ggpht.com/xpRnv7oOGXWKZzNZ1PqOiAz5s1cnA0Nd6dycoiwojULlMNK8ziqb9G_jc8kpD0IGewcFOrxJwBS04m23u2X9oPA
And no, you can't modify them.
However, you can write your own handler at http://your_app_id.appspot.com/i/ to redirect the requests to the actual image URL.

restricting access to files

I'm currently developing a picture-storage on MVC3 and have a question how to restrict access to images.
If I insert image into HTML lice <img src="/ImagesFolder/image0001.jpg"> it will be shown correctly, but anyone who write full path in browser will get that image too. I don't wand to permit it.
One way is to embed image as base64 string, but it is suitable only for small images, I have large ones.
I've seen recommendations to create image-accessing action, and use something like
<img src="/GetImage?ID=1123">, but at that GetImage page I will still use either direct-path or base64 methods? and in first way full path to imagefile will be translated into parent view and still can be seen in picture properties?
Is there a way to use System.Drawing.Image in <img src=""> or any other way? Do you know any samples?
One way to do this is put all of the images into a path which is not published by the http server. This way there is no direct path users can put into the browser.
Scripts themselves are on the server so they can access this path. Your image-access method should be a script which returns actual data and identify itself as the data it is serving image/jpeg for jpegs for example. This way if somebody accesses GetImage method by some other means than the one you have designed the script can detect it (by referer, or other means) and return nothing.
I think that the "image-accessing action" solution you mention is the easiest one to implement. The GetImage script could for example check for a cookie that you set in your application, so that only people that have first visited your site can receive the images.
Now, if you want to prevent people to display the images by typing the URL in the browser after they visited your site, this is more tricky and in my opinion there isn't an easy solution for that. You might check the Referer HTTP header to see if the request is related to your website (as Referer will be empty when putting the URL into the URL bar, but will contain the URL of the page containing the image when it is included with <img src=...>), but this solution has a drawback, as it will prevent browsers not sending the Referer header (generally for privacy reasons; it is not a very wide-spread configuration but this happens) from seeing your images at all. A second possible drawback is that people will probably be able to see the images that are already cached in your browser by typing their URL, unless you can configure the server to serve them with some headers disallowing caching.

MVC3 Using or Finding the ApplicationPath inside of the _Layout.cshtml page

I have a C#.Net web app and the pathing is different from my local box to the dev box. My local url is http://localhost:<port>/Proposal/Edit. However, on the dev server, it is http://{MydevServer}/dev/app/Proposal/Edit]. So, this causes issues with the Style Sheet and navigation links, etc. I know I can grab the ApplicationPath inside all the Controllers and set a variable whic the pathed elements can use. But that seems like too much work for this issue. Any ideas on how to solve this? Is it possible to get the ApplicaionPath in the _Layout.cshhml file? Is ther a better idea?
You can call Href("~/") in any Razor page to get the full client path to your application root.
you should be using the Url.Action and Url.Content helper methods for generating your links and src attributes. then you'll never need to worry about it.

MVC 3 Content and Relative Path is not working when deployed to IIS 7.5

I am developing a MVC 3 Web Application and just tried to deploy it on IIS 7.5
The page is shown, but all the images as well as url path is not working at all.
I search through a lot of sources and found out that it seems to be compulsory for a MVC application to have all path being enclosed in either:
#Url.Action
#Url.Content
#Html.Action
and so on. So I tend to change all my relative path by using those valid method.
I understand it should be a correct way to use all those valid mvc helpers, but I am just a beginner and this is my first web application. My question is:
Is it possible to use any method to "resolve" the relative path so that it can be found even after deployed?
Really need help here... thank you very much....
You are not correct that you need to use those helpers for all URLs. There is nothing to prevent you from outputting URLs 'manually', as literals, or as you have done above.
Something wonky has happened with the paths of the files on your server upon deployment, most likely.

Resources