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

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");

Related

ASP .NET MVC rewrite img src url

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.

In C# MVC4 how to get the server path?

In c# MVC4 how to I get the server path. for example: http://192.169.1.120:60632
Is there a helper function that I can convert something like ~/aFolder/file.htm into an absolute path? Ideally I would like a way of taking any given url and convert into a full absolute url. E.g. can cope with..
/aFolder/file.html -> http://192.169.1.120:60632/aFolder/file.html
http://website.com/file.html -> http://website.com/file.html
And will work anywhere within the c# code - i.e. in a action controller, signalR hub, model etc.
And it will still work when I deploy to a remote server.
Use the Request.Url property available in Controller. This returns a Uri object containing info on the request. From there, you can access the AbsoluteUri and Port properties to get the info that you need.
If you are interested in getting the url info from SignalR, try looking at this question and answer.
Try this one,
System.Web.HttpContext.Current.Server.MapPath(#"~/FolderName")
You can try these as well
string path = AppDomain.CurrentDomain.GetData("FolderName").ToString();
HostingEnvironment.MapPath(#"~/FoldeName");
In MVC, you can get a fully qualified URL using the fourth parameter of Url.Action - protocol:
Url.Action("Index", "Home", null, Request.Url.Scheme)

Remove Profile.aspx?Username=Mike and replace it with: /Mike

I am working on a social networking site. A user has an own profile-page.
A profile-page link example:
www.example.com/Profile.aspx?Username=Mike
Is there a way to remove the Profile.aspx from the link? (the Profile.aspx references to a Profile.cs)
And is there an other way to remove ?Username= ?
I just would like to have a simple and clear link like: www.example.com/Mike
Thanks in advance!
There are two possible method you could use:
If you are on .NET 4.0 or higher you can use routing (as already mentioned by #Arsen.
Another way would be to use URL rewriting. With URL rewriting you can tell IIS to process each incoming URL and map it to a different URL. More information on URL rewriting can be found here: http://www.iis.net/downloads/microsoft/url-rewrite

URL shortener using c#

I would like to shorten a url (e.g. http://www.abc.com/products/bag.aspx) to something like (http://short.me/bag). I have found that rules can be added to web.config to detect the short link to open the correct page. But I need a dynamic web.config. Is it a good idea to keep updating the web.config whenever a user creates the short url? Or is there a better way to do it?
I have tried YOURLS, RewriteRule, etc. All don't seems to work properly on my server. I am using a WIN server. I don't really want to use the bitly API as I would like to have my own domain in front of the link. Or is there a way to use the bitly API and still retaining my domain name?
Thank you.
You need to create a database mapping short URLs to long URLs.
You would then create an HTTP handler that looks up the short URL in that database and redirects to the corresponding long URL.
Then, register that handler to run for all requests.
Bit.ly lets you use custom domain names now: https://bitly.com/pro/products, check out this part of their FAQ

I have a question about URI templates in WCF Services

I have a web service with following operation contract and my service is hosted at http://localhost:9002/Service.svc/
[OperationContract]
[WebGet(UriTemplate = "/Files/{Filepath}")]
Stream DownloadFile(string Filepath);
This web service would let users download file, if the proper filepath is provided (assuming, I somehow find out that proper filepath).
Now, I can access this service from a browser by typing, http://localhost:9002/Service.svc/Files/(Filepath}
If {filepath} is some simple string, its not a problem, but I want to send the location of the file. Lets us say users want to download file C:\Test.mp3 on the server. But how can I pass C:\Test.mp3 as {Filepath}? I get an error when I type http://localhost:9002/Service.svc/Files/C:\Test.mp3 in the browser.
I am new to web services and find that this community is the quickest way to get answers to my questions.
got it working now.
need to use HttpServerUtility.UrlTokenDecode() from System.web
You cannot type special characters like ":" and "\" as part of a URL. They will need to be URL encoded.
Sounds like you need to URL encode the string, then decode on the back end. Add System.Web to your project and use System.Web.HttpUtility.UrlEncode() then System.Web.HttpUtility.UrlDecode()

Categories

Resources