www.yoursite.com/image/http://images.google.com.ph/images/nav_logo7.png
What I need to know here is the Controller Action, and the Global.asax routes
The colon : character is not valid in the path segment of the URL, so you'll have to either encode it, or remove it entirely. After that, you can use the {*routeValue} syntax to specify that the route value should be assigned the remainder of the URL.
routes.MapRoute(
"Image",
"image/{*url}",
new { controller = "Image", action = "Index" }
);
For the url http://www.yoursite.com/image/images.google.com.ph/images/nav_logo7.png
, the above route will execute ImageController.Index() with a url argument of "images.google.com.ph/images/nav_logo7.png". How you choose to deal with the protocol (encode/remove) is up to you.
Also keep in mind that a url authority can be made up of a domain name and a port number, separated by : (www.google.com:80) which would also need to be encoded.
If you want to send a URL as a parameter on a URL you need to URL Encode it first
In c# use Server.UrlEncode(string) from the System.Web namespace
So your example will look like:
www.yoursite.com/image/http%3a%2f%2fimages.google.com.ph%2fimages%2fnav_logo7.png
And your route pattern could be:
routes.MapRoute(
"image",
"image/{url}",
new { controller = "Image", action = "Index", url = "" }
);
I'd start by not trying to embed a second URL into your route.
In cases where I have to use a URL as part of a route, I replace the slashes with an alternate character so you don't have issues with the interpertation of the URL as a malformed route (i.e.~,|,etc.) then retranslate these with some string replaces in the controller. And if possible, I'd ditch the HTTP:// and assume the route is a URL by convention.
So your route would become something like:
www.yoursite.com/image/images.google.com.ph~images~nav_logo7.png
URL-encoded slash in URL - Stack Overflow
This is same problem and solved solutions.
1st solution.
Replace "://" to "/". Routing url pattern "image/{scheme}/{port}/{*url}".
2nd solution
"image/{*url}" set *url value base64.
Related
I have route:
config.Routes.MapHttpRoute(
name: "RestApi",
routeTemplate: "rest/{storage}/{controller}/{id}/{action}",
defaults: new
{
id = RouteParameter.Optional,
action = "Index"
}
{id} parameter can be URI itself, and I encode it. For example, route can be:
/rest/main/nodes/http%3A%2F%2Fwww.company.com%2Fns%2FGeo%23United_States/rdf
But this way wrong, it isn't work. With simple {id} parameter it is OK.
What I should do to make it works?
What I should do to make it works?
Just use query string parameters if you intend to send arbitrary characters to the server:
/rest/main/nodes/rdf?url=http%3A%2F%2Fwww.company.com%2Fns%2FGeo%23United_States
You may read the following blog post from Scott Hanselmann in which he covers the difficulties of using such values in the path portion of the url.
I quote his conclusion:
After ALL this effort to get crazy stuff in the Request Path, it's
worth mentioning that simply keeping the values as a part of the Query
String (remember WAY back at the beginning of this post?) is easier,
cleaner, more flexible, and more secure.
I have following route in my bundle
/{category}
And I have category with name
Category/Brand
If url is something like this:
domain.com/Company/Brand
then I get error 500 about / symbol.
But if in twig I do
company.name|url_encode()
then I get
Company%2F%Brand
(Code might be wrong, dont remember right now)
But nevertheless Symfony tells me that there is no route matching
And gives me 404.
How can I solve this problem?
Are all your category names like that one or only some of them?
If all of them follow this structure, you could change the route to:
/{company}/{brand}
And change the corresponding controller to accept two variables instead of one. Later you can concatenate them or do whatever you need with them
If only some of them have this structure, you could try to replace the directory separator with some character combination in the controller which creates the link and then reverse this replacement in the controller for this route. For example, in the controller for the template where the link is shown you could
$nameEncoded = str_replace ('/','%%%%',$companyName);
pass this variable to the template and use it to generate the link and then in the receiving controller do:
$nameDecoded = str_replace ('%%%%','/',$companyName);
If your route is /{category} and the URL that you type is domain.com/Company/Brand, the error is normal.
You have to config your route in routing.yml like this:
Company/{category}
Ok!
I found a solution here.
#PUBLICATION URL
###_publication:
pattern: /{username}/{category}/{publicationid}
defaults: { _controller: ###:Default:publication }
requirements:
_method: GET
category: .+
username: .+
publicationid: \d+
#CATEGORY - should be at very end, to match all other URLS
###_category:
pattern: /{category}
defaults: { _controller: ###:Default:category }
requirements:
_method: GET
category: .+
My MVC web application generates an activation link that can include any character (%,+,/,etc.). I URL encode the string and generate link:
new UrlHelper(HttpContext.Current.Request.RequestContext)
.RouteUrl("AccountActivation",
new { id = HttpContext.Current.Server.UrlEncode(activationString) };
then add the domain and it looks like:
http://localhost/AccountActivation/asdlkj223%25asd%2Basw3fgasdme
The URL is then passed to the user.
The route in this case is:
routes.MapRoute(
"ActivateAccount",
"AccountActivation/{id}",
new { controller = "Account", action = "Activate", id = ""});
It seem fine to me, but the ASP.NET development server and IIS give me HTTP Error 400 - Bad request. Which means there's a problem with the URL that I can't see.
When get rid of the {id} in the route description (I also tried {*id} with no success):
routes.MapRoute(
"ActivateAccount",
"AccountActivation",
new { controller = "Account", action = "Activate"});
the URLs look like:
http://AccountActivation?id=asdlkj223%25asd%2Basw3fgasdme
and they work just fine...
I though those 2 approaches do exactly the same thing. What is the difference between them? Is it the MVC engine that performs something more for me or I miss something with the URL encoding.
Try UrlPathEncode instead of UrlEncode - some characters are illegal in the path that are legal in a query string.
That said - I believe the analysis of whether a character is 'bad' is performed after path-decoding occurs; and is done by IIS. It will reject some characters because of the possibility that the URL maps to the physical file-system and therefore could allow a user access to things they really shouldn't have access to. Equally it's done to prevent requests from sending data that really shouldn't be sent.
Generally if there's operationally no benefit from having a parameter mapped as a route parameter, then don't try too hard to map it - especially in this case where the string could be anything.
By the way - if that's an encoding of binary data; you can instead consider hex-encoding it or using modified base-64 for URLs instead - which will not cause errors if mapped as a route parameter.
When the characters %20 appears in between paramaters a url, my MVC routing stops considering that a string.
Why is that, and how can I approach handling "%20" characters in my URL?
Example URL
http://localhost:40494/ListContents/Delete/asdf%20/5430f394...
public ActionResult Delete(string DNSName, Guid id)
{...}
routes.MapRoute(
"Delete", // Route name
"ListContents/Delete/{DNSName}/{id}", // URL with parameters
new { controller = "ListContents", action = "Delete" } // Parameter defaults
);
However
Both the following URLs work fine
http://localhost:40494/ListContents/Delete/asdf%20SOMETHING_HERE/5430f394...
http://localhost:40494/ListContents/Delete/%20asdf/5430f394-946c-4f82-ac13-9d5efafe9127
If an empty space is at the end of any section of the URL before the next slash, it throws a HttpException in the System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath() method which is handled by MVC and you'll get a HTTP 404 response.
You can verify that yourself by checking the checkbox for Throw in:
Visual Studio
Debug
Exceptions
Common Language Runtime Exceptions
Generally you should not have empty spaces in your URLs. I personally format my urls, that all spaces becomes a dash (-).
I think the problem is that in the example where it doesn't work is because it can't be parsed as a valid URL, it will be read as
http://localhost:40494/ListContents/Delete/asdf /5430f394...
Instead, you would be safe to just remove the %20 from that url safely.
Check if the id field of the table isn't a string (nchar(x)). If so, check if the respective id has the exact lenghth defined in the type declaration. If not (if it has less chars), that's the problem (it should have the EXACT lenghth you declared). This worked for me.
Can anyone tell me what is the syntax for retreiving the actual URL for the "Default" Route?
I'd like to do something like:
string url = RouteTable.Routes["Default"].ToString();
//(even though that code is completely wrong)
so that I could have the url value of the route available to work with.
So far, I've been trying the .GetVirtualPath() method, but that only returns the route data for the current controller.
thanks
Dave
A route can match any number of urls. So a route doesn't have a url. To get a url from a route you will have to supply it with the route data that you want the url for. To do that you simply use the RouteUrl() method on the Url property, Url.RouteUrl().
Update
If you want the url that will be generated if you supply the route with its default values you can do something like this:
var url = Url.RouteUrl("Default", ((Route)RouteTable.Routes["Default"]).Defaults);