Why I'm facing problems with #Href("path") and not with #Url.Content? - asp.net-mvc-3

I was writing a question asking why #Href isn't recognizing the path correctly.
E.g.
#helper CSS(string cssFile, string media = "screen") {
<link href="#Href("~/content/stylesheets/" + #cssFile)" rel="stylesheet" type="text/css" media="#media" />
}
is throwing sereval js 404 error trying to found the resource in
http:// localhost/content
http:// localhost/dashboard/content
and never what I want:
http:// localhost/MySite/content
So them I found the interesting part: #Url.Content instead of #Href works fine! Why!?
Therefore this is not the bigger problem, the issue is that in App_Code #Url doesn't exist in the context. What I can use then.

MSDN is your friend. From WebPageRenderingBase.Href Method:
When overridden in a derived class, builds a URL using the specified
parameters.
Whereas from UrlHelper.Content Method:
Converts a virtual (relative) path to an application absolute path.
The tilde in your path makes it a virtual path, which apparently #Href() doesn't understand.

Related

How to dynamically set the base tag?

We are using backbone routing, mod_rewrite, requirejs. The app is in a folder, not on the web root, so relative folder references are required for images,css, and js files (if we could use absolute folders the files would load).
When accessing a route with a trailing slash none of the js and css files loads correctly unless there is an appropriate base tag set in the header. Like so:
<base href="//localhost/myapp/" />
This solution works. The problem is we need to variablize the base tag so that we can have dev and production versions of the code. But to load a js file with the variable wont work without a base tag.
Just to be sure I did the standard fixes for backbone. Fix optional slash (/):
routes: {
'faq(/)':'jumpToText',
'register(/)':'jumpToForm',
},
And setting the root in history
Backbone.history.start({pushState: true, root: "//localhost/myapp/");
The problem appears to be an unresolvable mod_rewrite issue. So the final thought is to dynamically set the base tag.
We ultimately used JavaScript to parse the value out of location.href . Wrap this code in a script tag in the head:
document.write("<base href="+'//'+document.location.host +'/'+ location.href.split('/')[3]+'/'+" />");
And did the same in routes.js (parsing out the uri)
Backbone.history.start({pushState: true, root: "/"+location.href.split('/')[3]});
A working solution that I have to account for protocol / host / port is the following
var base = document.createElement('base');
base.href = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
document.getElementsByTagName('head')[0].appendChild(base);
This currently works fine in all major browsers including IE11 (Which does not support window.location.origin)
I have used this to make an npm package that also supports adding a suffix to the end of this base href if anyone is interested
https://www.npmjs.com/package/dynamic-base
https://github.com/codymikol/dynamic-base

Sharing Helper Funactions across Views in ASP.NET MVC 3

I have a Helper method that I need to use across multiple views. I tried to using the following information: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx. I have defined this Helper method in /App_Code/Helpers.cshtml. My views are located at
/resources/_Layout.cshtml
/Views/Index.cshtml
/Views/Home/Index.cshtml
If I reference my Helper function in _Layout.cshtml everything works fine. However, if I reference my Helper function in the other views, I get a runtime error. The runtime error says:
CS0103: The name 'MyFunction' does not exist in the current context
My Helper method looks like the following:
/App_Code/Helpers.cshtml
------------------------
#helper MakeUrlRelative(string url)
{
#MyNamespace.MyClass.MakeUrlRelative(HttpContext.Current.Request, url)
}
When I reference it in /Views/Index.cshtml, I use it like this:
<link rel="stylesheet" href="#MakeUrlRelative("/resources/css/app.css")" />
When I reference it in /Views/Home/Index.cshtml
click me
As you can see, I'm referencing the MakeUrlRelative method the same way. Its like the Helpers.cshtml file isn't accessible from /Views/Home/Index.cshtml. What would cause this?

How do I disable inclusion of passed down JavaScript and Stylesheet files in view.yml for a module?

In symfony1.1, I develop a module that shall not include any of the passed down javascripts and stylesheets of its application.
I hence I created a module-specific view.yml, yet I cannot find the syntax for disabling them.
My original question involved only JavaScript and CSS. But now I want to remove metas and http_tags as well. For some reason I get for:
all:
http_metas: [-*]
metas: [-*]
the actual tag
<meta name="0" content="-*" />
Does anyone know what's different here?
You can exclude all the passed down javascripts and stylesheets, or remove just specific ones.
For example:
indexSuccess:
stylesheet: [-style]
or:
indexSuccess:
stylesheet: [-*]
It does not seem possible for metas:
You may find yourself wanting to remove default meta tags for specific modules within your application. This isn't possible through view.yml or module.yml ... The solution is to extend the sfWebResponse class, overriding the getMetas() method. This allows us to filter out unwanted tags without affecting special behaviour, e.g. for the title tag.
class myWebResponse extends sfWebResponse
{
public function getMetas()
{
$meta_tags = $this->parameter_holder->getAll('helper/asset/auto/meta');
if ($this->getContext()->getModuleName() == 'special_module' && array_key_exists('bad_meta', $meta_tags)) {
unset($meta_tags['bad_meta']);
}
return $meta_tags;
}
}

ClientGlobalContext.js.aspx broken in Dynamics 2011?

I am trying to implement a custom web resource using jquery/ajax and odata. I ran into trouble and eventually found that when I call:
var serverUrl = context.getServerUrl();
The code throws exceptions.
However, when I change serverUrl to the literal url, it works. I then found forum posts that said I should verify my .aspx page manually by going to https://[org url]//WebResources/ClientGlobalContext.js.aspx to verify that it is working. When I did that I received a warning page:
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
Invalid at the top level of the document. Error processing resource 'https://[org url]//WebResources/Clien...
document.write('<script type="text/javascript" src="'+'\x26\x2347\x3b_common\x26\x2347\x3bglobal.ashx\x26\x2363\x3bver\x2...
What the heck does that mean?
Hard to tell outside of context (pun not intended) of your code, but why aren't you doing this?
var serverUrl = Xrm.Page.context.getServerUrl();
(Presumably, because you have defined your own context var?)
Also, this method is deprecated as of Rollup 12, see here: http://msdn.microsoft.com/en-us/library/d7d0b052-abca-4f81-9b86-0b9dc5e62a66. You can now use getClientUrl instead.
I now it is late but hope this will be useful for other people who will face this problem.
Until nowadays even with R15 there are two available ClientGlobalContext.js.aspx
https://[org url]/WebResources/ClientGlobalContext.js.aspx (the bad one)
https://[org url]/[organization name]/[publication id]/WebResources/ClientGlobalContext.js.aspx (The good one)
I don't know why exist 1. but it causes many issues like:
It could not be published or hold information (Your case #Steve).
In a deployment with multiple organizations, seems it saves info only for the last organization deployed causing that methods under Xrm.Page.context. will return info from a fixed organization. Actually each method that underground uses these constants included in ClientGlobalContext.js.aspx: USER_GUID, ORG_LANGUAGE_CODE, ORG_UNIQUE_NAME, SERVER_URL, USER_LANGUAGE_CODE, USER_ROLES, CRM2007_WEBSERVICE_NS, CRM2007_CORETYPES_NS, AUTHENTICATION_TYPE, CURRENT_THEME_TYPE, CURRENT_WEB_THEME, IS_OUTLOOK_CLIENT, IS_OUTLOOK_LAPTOP_CLIENT, IS_OUTLOOK_14_CLIENT, IS_ONLINE, LOCID_UNRECOGNIZE_DOTC, EDIT_PRELOAD, WEB_SERVER_HOST, WEB_SERVER_PORT, IS_PATHBASEDURLS, LOCID_UNRECOGNIZE_DOTC, EDIT_PRELOAD, WEB_RESOURCE_ORG_VERSION_NUMBER, YAMMER_IS_INSTALLED, YAMMER_IS_CONFIGURED_FOR_ORG, YAMMER_APP_ID, YAMMER_NETWORK_NAME, YAMMER_GROUP_ID, YAMMER_TOKEN_EXPIRED, YAMMER_IS_CONFIGURED_FOR_USER, YAMMER_HAS_CONFIGURE_PRIVILEGE, YAMMER_POST_METHOD. For instance method Xrm.Page.context.getUserId() is implemented as return window.USER_GUID;
To be sure that your URL is the correct just follow the link posted by #Chris

Using xsl:include with AJAX/XSLT

I am using Javascript to load XML and transform it with XSLT. Everything works until I try to use <xsl:include>. Is it possible to include files this way?
Here is the relevant XSLT code.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
...
<xsl:include href="another_xslt.xsl"/>
</xsl:stylesheet>
And here is the javascript code:
var xmlRequest = new XMLHttpRequest();
xmlRequest.open("GET", "data.xml", false);
xmlRequest.send(null);
var xsltRequest = new XMLHttpRequest();
xsltRequest.open("GET", "https://hostname.com/directory/xslt/transform.xsl", false);
xsltRequest.send(null);
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltRequest.responseXML);
return xsltProcessor.transformToDocument(xmlRequest.responseXML);
I am using Firefox 3.5 and the script fails on the xsltProcessor.importStylesheet line without any error messages.
Update:
Tamper Data shows a request for another_xslt.xsl but in the parent directory so it is getting a 404.
All the xslt files are in https://hostname.edu/directory/xslt/ but Firefox is requesting http://hostname.edu/directory/another_xslt.xsl instead. Why would it be requesting the file from the parent directory and without the https? The html file is in /directory/admin/edit
Using the full URL fixes the problem, but I need it to work on the server, so I would prefer to use the relative path like it is now.
It is possible to xsl-include with importStylesheet() so that shouldn't be the problem.
I do not really have the answer but perhaps some pointers to verify:
Do you have an infinite inclusion recursion where file x includes y which in turn includes x?
Make sure the xsl:include is not followed by an xsl:import.
If you use something like Tamper Data do you see FireFox requesting 'another_xslt.xsl' ?
Long shot guess: I never seen an xsl:include that wasnt defined straight under xsl:stylesheet before any xsl:template declarations perhaps moving this xsl:include declaration to the top might help (keeping in mind it xsl:import should go first).

Resources