I am using the following JavaScript code to refresh another page
window.opener.location.replace(url)
The problem is when entering the URL, do not find the page as the page is located in the root and this calling code is placed in a page inside another folder. How do I specify the path to point to the root, which is where the page is located? I have tried many things, but none worked:
//page.aspx
../page.aspx
~/page.aspx
page.aspx
/page.aspx
....
This form should work:
window.opener.location = url;
The replace method replaces a match in the current URL with another string, but it won't work with just one parameter. At any rate, it is not necessary to specify the full URL, you can only specify the partial path such as:
../page.htm
You would have to give exact path
Related
I'm working with an ASP.net web application.
I've written a user control called LocationSelector that has its own Javascript in an external .js file. In order to load that file, I use the following line of code:
ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), "Controls/LocationSelector.js");
The problem is with "Controls/LocationSelector.js". As long as the page that uses the control is in the root directory of the application, everything works. However, as soon as I try to put this control in a page in a subdirectory, it can't load the Javascript file.
How can I fix this?
Haven't tested it, but off the top of my head I would say you need something along the lines of this:
ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), Page.ResolveClientUrl("~/Controls/LocationSelector.js"));
I have a page on my site, for purposes of this example lets say http://myhost/Pages/Default.aspx. On Default.aspx I have a user control called MyControl.ascx which lives in /Controls/MyControl.ascx. So the tree looks something like this
Root
Pages
Default.aspx
Controls
MyControl.ascx
Whenever I place a HyperLink control on MyControl.ascx and specify a NavigateUrl, this path is relative to the control, not the URL of the page. So for instance if in NavigateUrl I specified "AboutMe.aspx", the URL would be rendered as http://myhost/Controls/AboutMe.aspx instead of http://myhost/Pages/AboutMe.aspx. Is there any way I can make this relative to the page URL? I've tried the solution here: Relative path from an ASP.NET user control NavigateUrl but it didn't work for me.
Edit
To clarify, I'd like this to be generic enough so that if I didn't know what the path was the solution would work. So I don't really want to harcode "~/Pages/Default.aspx" in the NavigateUrl
You have a bunch of options:
You could hardcode the Url using the ~ operator which give you the root and then define it from there like: ~/Pages/AboutMe.aspx. Keep in mind that the ~ operator is recognized only for server controls and in server code.
You could also use the .. to get you to where you want as it will navigate back up the folder structure like: ../Pages/AboutMe.aspx
You could create a helper methods to return you a valid root to your Pages folder, Images, Javascript, etc...
The HttpRequest.ApplicationPath will get your the virtual application's root path on the server which you can use to build off.
For more information on Pathing options you should read this article on MSDN:
ASP.NET Web Project Paths
EDIT: Based on your edit to your question, you should then pass in the relative URL into you user control via a property. Let the page that uses the control define the relative path to the resource it will require.
The best way to achieve this is to use
string.Format("{0}", Page.Request.Url.PathAndQuery);
in NavigateUrl property
Just in case anyone else stumbles across this as I did...
In the code behind, I set the NavigateUrl to:
link.NavigateUrl = Page.ResolveUrl("aboutus.aspx");
How do you set the NavigateUrl of the hyperlink? If I set it using the ~ character, then it works as expected:
MyControl.ascx
<asp:HyperLink runat="server" NavigateUrl="~/Pages/Default.aspx" ... >
Simpler than that. Just change the usercontrol's AppRelativeTemplateSourceDirectory property to the value of the same property of the parent Page.
protected override void OnInit(EventArgs e)
{
AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory;
base.OnInit(e);
}
then all relative path in user control will be if there was in page.
I have a jQuery function in a .js file, and the function requires a querystring parameter to be passed.
If I try to use the CompositeScript feature in the ScriptManager, it pukes on the file that has the querystring. Is there any way around this?
I was trying to do the same thing and the error I got was something like "URL was not a virtual path" so I'm assuming the path must be virtual. So you may not be able to do this as appending the querystring variable to the path will cause the URI to choke.
You could set your variable in the page or masterpage if global, thus you wouldn't need to pass a querystring.
Might be a way to do what you are accomplishing, but my guess would be that you would have to add in another step, like a route to the jsfile.js?qv=whatever to jsfile.js, or add in an httphandler. Probably more trouble than it is worth.
Just guesses.
Short:Is there a way to have a route-definition that will pass the "CONTROLLER/ACTION" string value to a JavaScript function in stead of actually going straight for the controller action?
More:I have a masterpage which contains the navigation of the site. Now, all the pages need this navigation wrapped around it at all times, but because I didn't want the navigation to constantly load with each pagecall, I changed all my pages to partialviews.
These partial views are loaded via the JQuery.Load() method whenever a menu item is clicked in the navigation.
This all worked very good, up till now because I noticed it's also a requirement of the website to be able to link directly to page X, rather then default.aspx.
So, as an example:The main page is my "default.aspx" page, this utilizes my master page with the navigation around it. And each call to a new page uses a javascript function that loads that particular partial view inside a div that is known in my masterpage. So, the url never changes away from "default.aspx", but my content changes seemlesly.
The problem is, those url's also need to be available when typed directly into the address bar. But, they're partial views, so loading them directly from the address bar makes them display without any masterpages around them. Therefore my question if it might be possible to capture the route typed into the address bar and pass that on to my JavaScript function that will load that route in the content div.
(I hope I explained it ok enough, if not, feel free to ask more information)
You are 100% correct to not want to hard code your URLs in your javascript code as it demolishes one of the primary tenants of MVC to do so. I'm one of those "separation of concerns" guys who will not write a single line of javascript outside of a dedicated .js file so I cannot dynamically specify the URL the way tuanvt has. What I do is use MVCs Url.Action method to emit my service URLs into hidden inputs on the master page (or the specific page if it is not used in multiple places). Then my script file simply pulls the value out of that hidden input and uses it just fine.
ASP.NET MVC View Source
<input id="serviceUrl" type="hidden" value="<%= Url.Action("Action", "Controller") %>" />
JS Source
$.getJSON($("#serviceUrl").val(), function(data) {
//write your JS success code here to parse the data
});
First challenge, as you are using AJAX to load the partial pages you need client accessible URLs for the javascript to call. Second challenge, you need URLs that will load the HomeController and pass the 'page' portion of the URL into the javascript.
For the first aspect I'd create some abstracted routes, i.e. "/ajaxaccess/{controller}/{action}/{id}" for the partial pages. That would be the first registered route. A second route would accept any controller/action reference and always get processed by the HomeController Index action.
In the /Home/Index action you grab the requested URL and slice it up, take the /{controller}/{action}/... section and pass that into your default.aspx using TempData. In your page check for the existence of the TempData and if it exists use the value therein to trigger your AJAX page load for the partial page (don't forget that you'll need to prepend '/ajaxaccess' (or whatever you choose) to the URL before it's passed to your page.
I'm not going to provide code here as I think the information you'll gain from working through this yourself will be invaluable to you moving forward.
You could use hash anchor (#) on your url and read it with javascript.
var url = document.location.toString();
if (url.match('#')) {
anchor = url.split('#');
// do whatever with anchor[1] ..
}
You can do something like this, put this in your javascript code on the view:
var szUrl=<%= ViewContext.RouteData.Route.ToString()%>;
Then the current route will be stored on the variable szUrl.
By convention, all my web site's .aspx files also have corresponding .css files at the same path. So, for example, Default.aspx has a file Default.css in the same directory.
I wrote an extension method to add CSS tags to the headers of Page objects, and use it like this on Page_Load:
this.AddCssFileRange(new[]
{
"Default.css",
"../Master.css"
});
I'd like to replace the hardcoded "Default.css" with a method that derives this based on my CSS convention. That is, I'd like to replace it with a method that returns "Default.css" because the filename of the Page calling it is "Default.aspx."
How can I retrieve that "Default.aspx" filename so I can replace the extension with "css"?
Like this: Path.ChangeExtension(Request.CurrentExecutionFilePath, ".css").
This will return the currently executing page, even if you called Server.Transfer.