ASP .NET MVC rewrite img src url - c#

I have images in my web app that are currently retrieved from another domain:
<img src="https://images.other.net/{folder}/{filename}"/>
I want to rewrite all image urls to a specific action in my own domain but using some parts of the path above. The new URL should look like:
<img src="https://images.mydomain.net/en/home/getimage?name={filename}&folder={folder}"/>
where folder and filename are dynamic values.
I want to see if I can do it without actually changing all paths 1 by 1.
Can I do it either by MVC Routing rules or by URL rewrite rules in the web.config file?

Below is the list of several options to do that.
This list also contains the benefits/problems you will face using these methods:
Changing all paths:
You can change all the paths at once by using the find and replace option if the base URL of all the paths is the same.
The shortcut key for Find and Replace option is Ctrl + Shift + R .
If you will keep the base URL in multiple files/places in the code, it will create a problem for you if the URL will change in the future, you have to replace them again.
Put the base URL in a web.config key (Recommended)
Better approach is to create a key in the web.config file and put the base URL there so that even if it changes in the future you just have to change the URL once in the web.config file.
Using IIS URL Rewrite
You can get help from this article to create some URL rewrite rules.
For rewrite rule with dynamic values see this article.

Related

How to add an download link to users in my project

In my project i will be having an link like
Download
I want the users to download files of different types. The file will be in the root folder. When i am clicking on the link it is displaying an error. This is the plugin to install in the chrome. If the user download this link and open then it will automatically add to the chrome.
How can i do this.
The file is not even downloading.
This isn't a valid path:
~/hello world.crx
The ~ character is for use server-side to denote the root of the application. Client-side it has no meaning. The browser doesn't know what the root of the application is (or what the application is at all), it's just sending requests to resources at addresses. And it doesn't know what to do with that address.
You'll need to either use some server-side logic to translate that path into a browser-useable path, or manually make it a relative or absolute path.
If the ASP.NET MVC Framework isn't translating this for you then you're probably using a version that requires a little more manual work for it. Try something like:
Download
(Note: This assumes the use of the Razor view engine. If you're not using that then you'll want to use whatever your view engine equivalent is.)
What you need to do is set up a directory online, where you can host the file.
I also see that in your aref you don't want to type the full path so denote it with a /hello_world.crx, but make sure that you've set up a base href:
<base href="http://yourdomain.com/something/">
Try renaming the file to remove any spaces e.g. "hello_world.crx" and then change the name in the link code to match.
if a webpage and the downloadable file is in the same location
(i.e)
SampleFolder->Download.html
SampleFolder->hello world.crx
then try the below
download
If the webpage and the downloadable file in different location
(i.e)
SampleFolder->Download.html
SampleFolder->Downloads->hello world.crx
then try the below
download

URL rewriting and html canonical link in the header to avoid duplicate page issue for search engines

I am using Intelligenicai.URLRewriter.dll to rewrite my website URL it is working fine i followed the following link as an example
http://anupkumargupta.wordpress.com/2011/05/28/url-rewriting-with-urlrewriter-net-simplest-way-asp-net-c-sql-blog/
On the other article i read about URL rewrite can create problems for search engines as now we can access same page with two different URL one without URL rewrite and one with URL rewrite. (Check this link http://thecodebug.com/?p=296)
Above mentioned link talks about search engines can penalize if they find duplicate contents on the website. This article (http://thecodebug.com/?p=296) talks about "html canonical link in the header" if we don't add them then we can tell search engine that this url is canonical.
Now my question is this applicable to even if i use Intelligenicai.URLRewriter.dll and if this is the fact what are the precautions i should take to avoid such scenario.
Example of my URL
With URL Rewrite
http://www.xyz.com/Article/en-US/19/87/let-the-spirit-of-our-nation’s-founders-guide-us.aspx
URL without rewrite
http://www.xyz.com/Article/ArticleDetails.aspx?Language=en-US&PageID=19&ArticleID=87
Part of web.config
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/>
</rewriter>
You can add 2 rules: One rewriting the new-style links to the physical URLs then stop processing rules, and then follow it with a rule to rewrite direct requests to the aspx page to the new style URL, 301 permanent redirect, and stop processing rules. The 301 redirect is important! If you can't get it to do that with your rewriting DLL then you can redirect the URL to a special hidden page that will accept the old page as the querystring or looking at the referrer, then mapping it out to the new page using a 301 redirect there.
UPDATE: Here's some more information:
Using Intelligenica UrlRewriter, you would set up two rules, in this order:
The first rule uses the rule you have already set up for redirecting the fancy URL to the physical file.
The second rule takes the path to the physical file and either (a) 404 or (b) 301 permanent redirects to the fancy URL. If you have a single page serving up multiple pages of content, you may be better off just stopping processing with a 404 error. Also, as long as you never use the old-style links anywhere in your site, you should be okay. If at ANY TIME you used the old style links (before you rewrote the URLs), you definitely need to make sure that you have rewriting set up to redirect or 404 the page so the new style URLs are forced.
<rewriter>
<rewrite url="~/Article/(.+)/(.+)/(.+)/(.+).aspx" to="~/ArticleDetails.aspx?Language=$1&PageID=$2&ArticleID=$3" processing="stop"/><!-- rewrites URL -->
<rewrite url="~/ArticleDetails.aspx?Language=(.+)&PageID=(.+)&ArticleID=(.+)" to="~/Article/$1/$2/$3.aspx" processing="stop" permanent="true" /><!-- Redirects old page to new url with 301 -->
</rewriter>
Hope this helps.

How to have a route in ASP.NET MVC with a file extension that would be blocked by request filtering

I have a route that basically looks like this:
Foo/{id}/Files/{*path}
I want it to match a URL such as:
http://mysite/Foo/Bar/Files/Baz.cs
However, I get an HTTP 404.7 error that says "The request filtering module is configured to deny the file extension." I can remove the file extension from request filtering by modifying web.config, but I don't want my source code to be viewable. How should I set this up?
The only way you might have possibility to get it working is through relaxedUrlToFileSystemMapping setting in ASP.NET 4.0. You can read more here:
http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx
If the paths to the files don't match with the urls you want to have than you can do the following:
mark the folder where you keep your source files as denied for everyone in the web.config (example for the /SourceCode dir)
allow the .cs extension in the request filtering module

Using forward slashes ('/') in URI templates without encoding

I've got a RESTful WCF service that acts as a file store. Since there can be any number of directories and sub-directories, I'm trying to let the users access them by simply putting the file path into the URL. Is there a way I can do this without requiring the user to encode the slashes?
For example, what I want is a URI template of Files/{path} that can be accessed like http://localhost:8000/Files/folder1/subfolder2/subfolder3/file.jpg.
You can put a * at the end of your uri template. Inside your operation you can interpret the rest of the uri that matches to * as your file path.
you'll need to use URL rewriting techniques to accomplish this. if using .net 4 use Route table.
I'm afraid not understanding right. Would the following work?
Let the user enter something like folder1/subfolder2/subfolder3/file.jpg in path variable.
Perform: String encodedPath = path.Replace("/", "%2F");

How to automatically generate the site map path in page header?

I used web.sitemap to generate the site map path for my asp.net application.
and I can generate two lays just like:
http://localhost:8080/test.aspx
but If i need to generate the MVC path like this:
http://localhost:8080/test.aspx/edit/2
and I need to know the "2" to get the site map.
Is there any method that I can use wild card
http://localhost:8080/test.aspx/edit/*
and then for this kind of path, system will auto generate the path at the page header ?
http://mvcsitemap.codeplex.com/
IMO it's not possible. The default provider of Sitemap is static. You've to write a dynamic sitemap provider to generate the sitemap nodes from the data source.
http://www.codeproject.com/KB/aspnet/dynamicsitemap.aspx

Categories

Resources