MVC3 - Getting the actual page path rather than Route - asp.net-mvc-3

I have a route as
http://www.domain.com/Test/12345678
This points to
http://www.domian.com/SomeArea/SomeController/SomeAction?customerId=12345678
Request.RawUrl gives the path as http://www.domain.com/Test/12345678
Request.Url.AbsolutePath gives the path as /Test/12345678
Request.Url.LocalPath gives the path as /Test/12345678
Request.Url.AbsoluteUri gives the path as http://www.domain.com/Test/12345678
So, how do i get the actual page path that is currently displaying as:
/SomeArea/SomeController/SomeAction

I think you want Url.RouteUrl

I think Request.FilePath is what you are looking for.
If you want the PathInfo trailer you can use Request.Path

Related

Web API - pass double parameters to GET method

I have the following method:
[Route("GetEditorialRequestsByCoordinates/{lat:min(-90):max(90)}/{lng:min(-180):max(180)}")]
[AutomapperExceptionApiFilterAttribute]
public HttpResponseMessage GetEditorialRequestsByCoordinates(double lat, double lng)
{
}
It works fine when I call...
GET /v1/api/request/GetEditorialRequestsByCoordinates/48/2
But I want to pass double value like this:
GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777
I got an error (404 not found). Seems, it can't find appropriate method by route.
I have tried to set route by way:
[Route("GetEditorialRequestsByCoordinates/{lat:double:min(-90):max(90)}/{lng:double:min(-180):max(180)}")]
...but it does not work either.
How can I fix it?
Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a 2.777 file extension, rather than an input parameter.
Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link.
he easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:
routes.AppendTrailingSlash = true;
For more details check here.
last of all max and min function works for integer
max: Matches an integer with a maximum value. {x:max(10)}
I think it does not work for double.

url::to(xxx/yyy) returns different results depending on context

I'm using the URL::to call to embed a link in an outgoing mail message. What I get when I do this is something like: "baseroot/public/index.php/xxx/yyy".
And yet when I do the same call, for example, within a route call, I get "baseroute/xxx/yyy".
Any idea?
The source of URL::to resides at
http://laravel.com/api/source-class-Illuminate.Routing.UrlGenerator.html#76-98
(linked to from http://laravel.com/api/class-Illuminate.Routing.UrlGenerator.html).
I suggest you add debug printing to your copy and see what values $this->getScheme() and $this->getRootPath() yield. These must be the source of the discrepancy, apparently caused by different this objects.
I had a very similar problem with URL::to('user/123') returning an incorrect value when visiting the homepage vs. another page. After some investigation, in my case it was a matter of case-sensitivity (!) in the request's url. I hope it's somehow related to your mysterious case.
More about my case: URL:to('user/123') gave me different results whether I visited http://localhost/MyApp/public/someurl or http://localhost/Myapp/public/someurl. In the former it gave the correct result http://localhost/MyApp/public/user/123, while the latter gave the wrong result of http://localhost/user/123.
.
From here, less important notes from my investigation, for future Laravel archaeologists. I hope I'm not talking all nonsense. I am new to Laravel, using a local Laravel 4 installation + WAMP on a Windows machine.
UrlGenerator's to() method uses $root = $this->getRootUrl($scheme);. The latter uses $this->request->root();, where request is \Symfony\Component\HttpFoundation\Request.
Request::root() indeed defaults to a wrong value e.g. http://localhost when visiting someurl with the incorrect case.
The culprit is Symfony\Component\HttpFoundation\Request (in vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\Request.php). Its getBaseUrl() calls prepareBaseUrl(), and there the actual logic of comparing the requestUri with the baseUrl is finally performed.
For the few archaeologists still following, in my case the $baseUrl was /MyApp/public/index.php while the $requestUri was /Myapp/public/someurl, which sadly led the code to not satisfy this conditional:
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, dirname($baseUrl))) {
return rtrim($prefix, '/');
}

System.IO.File.Exists() returns false

I have a page where I need to display an image which is stored on the server. To find that image, I use the following code:
if (System.IO.File.Exists(Server.MapPath(filepath)))
When I use this, I get a proper result as the file is present.
But when I give an absolute path like below:
if (System.IO.File.Exists("http://myserever.address/filepath"))
It returns false.
The file is physically present there, but I don't know why it's not found.
The path parameter for the System.IO.File.Exists is the path to an actual file in the file system.
The call to Server.MapPath() changes the URI into an actual file path.
So it is working as intended.
You can't use HTTP paths in File.Exists. It supports network shares and local file systems. If you want to do this in a web application on the server side. First use Server.MapPath() first to find the physical location and then use File.Exists.
Read about Server.MapPath here: http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx
Eg.
string filePath = ResolveUrl("~/filepath/something.jpg");
if (File.Exists(Server.MapPath(filePath)))
{
//Do something.
}

get application path codeigniter with out contents after last slash

hi i am using codeigniter in my application .
for my application i need application path only before the last slash
like this
if application path is
http://localhost/elephanti/connections/fb_connection/invite_friends
i want to get
http://localhost/elephanti/connections/fb_connection
it is for a plae like this
echo "<script type='text/javascript'>top.location.href = '"+APPPATH+"'testapp';</script>";
i tried to use '"+APPPATH+"'../testapp' but could not , please help ...................
If you know the last part will always be the 4th, you can use a "dirty way" like this:
$url = site_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3));
And then:
echo "<script type='text/javascript'>top.location.href = '".$url."'/testapp';</script>";
// here, though, I don't understan how "testapp" goes into the url..as a segment?
This will create a valid CI url using only the first 3 segments.
Alternatively, you can use the uri_string() function, wich returns only the segment part of the url. On this, you can explode/str_replace/array_pop/do whatever you need to and pass the new elaborated string on the site_url() function wichi will build the correct URL for you.
I don't understand what "testapp" is there or, do you want to substitute the last segment of your url with that? So that you have, in your example, http://localhost/elephanti/connections/fb_connection/testapp ?
Keep in mind that both uri_string() and site_url() are functions from the URL helper so you need to load it.
In Codeigniter APPPATH referes to the location on the disk and not the url of the application. What you are looking for is base_url(). base_url() return the base location of your site, which usually is the domain.
Concatenation in php is done with the dot operator: "."
echo "top.location.href = '".base_url()."'testapp';";

ResourceManager.GetString() not returning value

I am trying to implement multiple language support for my web project. I am new to it.
I am not able to get the resource file value by using ResourceManager.GetString() function. I am passing the name and current CuluralInfo. The resource file present in my App_GlobalResources are Sample.resx, Sample.en-us.resx, Sample.zh-cn.resx and Sample.ar-sa.resx. I am having a name field named "Heading1" and its value in all the resource files
My code is like
string Heading1= Resources.Global.ResourceManager.GetString(("Heading1", Thread.CurrentThread.CurrentCulture);
But it is always returning null value. Please help me to get the solution for this problem
Thanks
San
I found the problem
The code should be like
string Heading1= Resources.Sample.ResourceManager.GetString(name, culture_object);

Resources