Get "Default" Route Url in Controller - asp.net-mvc

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

Related

Match double slash in rails route constraint

Suppose I am expecting a url as part of my route - maybe a callback url or similar - I might use the following route:
get '/mymodel/:url', to: 'mycontroller#docallback', url: /.*/
Now I would like to be able to go to http://www.myapp.com/mymodel/http://www.google.co.uk/ and process http://www.google.co.uk/ in mycontroller - but it is processed as http:/www.google.co.uk/ (one slash). How can I rectify this? Is the regex wrong or is there some flag I have to set?
I don't think that "http://www.myapp.com/mymodel/http://www.google.co.uk/" is a valid url.
Normally if you want to pass a url as a parameter you would call CGI.escape on it first, which would convert "http://www.google.co.uk/" to "http%3A%2F%2Fwww.google.co.uk%2F" CGI.escape will turn any string into a url-safe version of itself, basically replacing any characters which have a special function in a url, like ":/?&" and also space and some other characters which would otherwise break the formatting.
So, you would end up with a url like
"http://www.myapp.com/mymodel/http%3A%2F%2Fwww.google.co.uk%2F"
which would come through in params like
params = {:url => "http://www.google.co.uk/"}
Note how it's been unescaped here: Rails automatically* calls CGI.unescape on parameter values before putting them into the params hash.
However, this url
"http://www.myapp.com/mymodel/http%3A%2F%2Fwww.google.co.uk%2F"
looks pretty weird to me. It would be better to be more explicit and pass it through as a named parameter in the url itself, like
"http://www.myapp.com/mymodel?url=http%3A%2F%2Fwww.google.co.uk%2F"
which will require a slight change to your routes.
* I think Rails will do this but it might depend on circumstances. Try it.
...Turned out that the request was not encoded on the client side before being sent, solution was to use encodeURIComponent() on the url before sending it.

Url params encoding

I have a route like this
/api/service/:id
But id is a string like name.of.something
So the the url will look like:
/api/service/name.of.something?other=parameters
The controller can't parse that request correctly because of dots.
How should I decode the id to pass it to the route?
You need to add a constraints option to the route that will cause it to accept periods. Note that this will break Rails' automatic format detection, so you will have to pass an explicit ?format=json to the URL if you specifically need format selection.
get "api/service/:id", to: "some#endpoint", constraints: {id: /[[:alnum:]_\.-]+/i}
Adjust the regex to your preference.

Is there a better way to reference a route by name in a link with AngularDart?

I have many named routes in my AngularDart app. I create links the old fashioned way, like this:
Go
That seems brittle. If I change the path or change the strategy away from hash change, I need to change all my links.
Can I do something like:
<a ng-link="activities">Go</a>
Where activities is the name of the route from my routes config.
For now you can use router to generate those URLs for you.
router.url('activities', {});
The second parameter (should probably be optional) is a map of parameter values. For example, if you have a path like /activity/:activityId then you can do:
router.url('activity', {'activityId', '12345'});
URL generator also honors current state of routes, so lets say you had an active route like foo.bar.baz, and foo was parameterized and you somehow got a hold of bar route (ex. via RouteProvider or queried router.root.getRoute('foo.bar')) then you don't need to know the values of the foo route parameters to generate the URL for baz, you can do:
Route bar = router.root.getRoute('foo.bar');
router.url('baz', {}, startingFrom: bar);
For now you will need to manually insert the generated URL into the template:
link

ASP.NET MVC AnchorLink not using route values?

I'm using AnchorLink on a very simple site with just two routes defined, one standard route and another area route for admin/{controller}/{action}/{id}. I'm at a URL like:
/admin/release/push/255
In that view I'm using:
#Html.AnchorLink("Confirm", "Confirm")
AnchorLink is rendering a link without the current request {id} included, ie. /admin/release/confirm! If I inspect the RouteValues I can see {id} is in there. If I explicity pass in the route values from teh current request, as in:
#Html.AnchorLink("Confirm this release", "Confirm", Url.RequestContext.RouteData.Values)
Then it works, I get the URL /admin/release/confirm/255. Why does the explicit version where I pass in the current request route values work, but the implicit one, without the route values argument, which I thought would pick up the current request route values, doesn't? I know the above is a solution, but it's ugly and there's some underlying reason why the AnchorLink without the route values isn't working as I expect?!
MVC is doing exactly the right thing here. It's not to know you require the Id parameter for your anchor link or not -- in other words its not trying anything clever to pre-parse and examine the link. It will only do that when its being rendered. And in your case without the id parameter specified somewhere its going to use the default route.
If you find yourself writing that same code all over the place you could easily extract it out into a static helper class. That can get rid of the ugliness.

how will you do this in MVC custom route?

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.

Resources