Embedding resource in DNN module gives 404 - asp.net

I'm trying to embed a javascript file in a DNN module, the script tag is created but the content is a 404 error.
The namespace of my project is "Carroussel" the location of my file is "js/jquery-carouFredSel-6.2.1.js"
I've added the following code to AssemblyInfo.cs
[assembly: WebResource("Carroussel.js.jquery-carouFredSel-6.2.1.js", "application/javascript", PerformSubstitution = true)]
And the following to the OnInit of the module:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.ClientScript.RegisterClientScriptResource(GetType(), "Carroussel.js.jquery-carouFredSel-6.2.1.js");
}
The scripttag is created in my html:
<script src="/WebResource.axd?d=gnpD_kHZKrDN3DKUCn2faE-x6tdus2cDJ1HjymfAToCQgqX2ggJno51OH2VqPx8ArZPoUm5RuWxhg8uAOTZaysKJE7jz3kpB6gHWbD25o2plphslAJao3Rs5ybJz_M9vLB1NmgPujgOBCt3pDGs9aY_lTy04oPXMqX6dyz0AHhjrbZlKl8muO0ZRGnAiWwdicmeAlg2&t=635349640675857733" type="text/javascript"></script>
But as soon as I view the source i get:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /WebResource.axd
Any ideas on what to change to make this working?

I would recommend that you not embed it, but let DNN handle the registration/loading of the resource. Doing so will allow you to easily see references to the JS if you have CDF disabled in DNN, and mask the references and have the content minified with CDF enabled in DNN.
Here is how to reference the client resource manager in DNN.
override protected void OnInit(EventArgs e)
{
DotNetNuke.Framework.jQuery.RequestUIRegistration();
ClientResourceManager.RegisterScript(Parent.Page, "~/Resources/Shared/scripts/knockout.js");
ClientResourceManager.RegisterScript(Parent.Page, "~/desktopmodules/DnnChat/scripts/moment.min.js");
ClientResourceManager.RegisterScript(Parent.Page, "~/desktopmodules/DnnChat/scripts/DnnChat.js",150);
base.OnInit(e);
}
pulled from https://github.com/ChrisHammond/dnnCHAT/blob/master/View.ascx.cs

Related

url rewrite in asp.net for dynamially generated urls

I have few dinamically generated urls like http://localhost:35228/begineercontent?name=lanaguages&id=23, I used routing to hide .aspx extension and now i want to see above url like this http://localhost:35228/begineercontent/lanaguages/23 i tried few url rewrite methods from iis url rewrite tool nothing worked kindly please help me out
This can be done very easy. You need to change/add this code in your global.asax.
void registerroute(RouteCollection routes)
{
routes.MapPageRoute("begineercontent", "begineercontent/languages/{Id}", "~/begineercontent.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
You also need to include the right assembly in global.asax by adding
using System.Web.Routing
In your begineercontent.aspx you need to add this code to the page load function.
var Id=( Page.RouteData.Values["Id"] as string);
A more detailled tutorial on how to work with routes can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/cc668177%28v=vs.100%29.aspx

Can't see an added class

I created an ASP.Net project in .Net 2.0 and uploaded all files to the server (dlls and cs files) to the server. It is a very basic HTML page with a button on it.
When I loaded it on the server, it was okay.
I added a static class to my project and used it when the button was clicked:
protected void btnUse_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtData.Text))
Utils.SaveData(txtData.Text);
}
When I uploaded it, I get the following error:
Server Error in '/' Application. Compilation Error Description: An
error occurred during the compilation of a resource required to
service this request. Please review the following specific error
details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'Utils' does not exist in the
current context
What else should I upload?
The code change can't take effect until the code is compiled, and the result is the .dll file(s). So two things to take care of here:
In order to add the class, you need to add it to the source code (which you've done), re-compile into the .dlls, and upload the new .dlls to the server.
You don't need (and shouldn't have) the .cs files on the server. That's the source code, not the compiled result. The compiled result is the .dlls. The server needs those, the markup pages (.aspx, .ascx, etc.), and any other resources (images, JavaScript files, .css files, etc.), but not the C# source code.

using dll extention instead of the standard aspx

I am replacing an existing web application, that all it's requests go through a url:
www.something.com/scripts/xxx.dll?args
I created my own aspx page that handles these requests and it is called:
www.something.com/scripts/xxx.aspx?args
My problem is that there are many existing links, from other website that refer to the xxx.dll?args url.
Can I create my own dll in .net that will receive the xxx.dll?args requests and process them?
This isn't a simple redirect, because I also need the args
I'd suggest to rather use Url Rewriting
After some more investigation I did the following.
Change the web.config, to make sure all requests go through my code by adding the following code:
...<system.webServer>
<modules runAllManagedModulesForAllRequests="true">...
Added a global.asax file to the web project, and within it wrote the following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Path.EndsWith("xxx.dll",
StringComparison.InvariantCultureIgnoreCase))
Context.RewritePath("/scripts/xxx.aspx");
}

Reasons why WebResources cannot be found

Hi I am having trouble getting an embedded js file to work.
I have tried all of the following:
Running Cassini development server (VS2008, .NET 3.5)
Added [assembly: WebResource("MyNamespace.MyScriptFile.js", "text/javascript")] above the class's namespace declaration.
Script file has build action "Embedded Resource".
Tried registering during OnInit, OnLoad and OnPreRender
Script file is in the same assembly and namespace as the control registering it.
Opened assembly with Reflector and verified the name of the resource is correct.
Does not work using any of the following methods:
ScriptManager.RegisterClientScriptResource(Page, GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptInclude(GetType(), "key",
Page.ClientScript.GetWebResourceUrl(GetType(), "MyNamespace.MyScriptFile.js"));
Other WebResource.axd files are being found - only this one is not being found.
The request for the resource returns a 404 page with an exception listed: "*[HttpException]: This is an invalid webresource request.*"
Using the ScriptManager.RegisterClientScriptResource produces the exception:
"*Web resource 'MyNamespace.MyScriptFile.js' was not found.*"
In your example code, you are making a call to GetType()... the type is used as the starting point for the search. Depending on where you are making your call to GetType(), you may not be getting back what you expect. Since ASP.NET dynamically compiles types for your pages and custom controls, GetType() may be returning a type defined in a new assembly built by ASP.NET.
You could try doing typeof(SomeType) instead, where SomeType is appropriate based on the location of your resource (ex. the control type you're working with).
maybe your resource file is located inside folder(s) in project?
if so, you should specify another name/location string in assembly and when you register the script
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.Folder1.Folder2.MyScriptFile.js");
[assembly: WebResource("MyNamespace.Folder1.Folder2.MyScriptFile.js", "text/javascript")]
usually that's a common problem
Try implementing your own ScriptManger and then adding the embedded file from there. Here's an example
public class MyScriptManager : System.Web.UI.ScriptManager
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RegisterClientScriptResource(this, typeof(ScriptManagerExtension), "MyNamespace.MyScriptFile.js");
}
}
An alternate cause of this issue - you've been too heavy handed with your Global.asax, and you've said that everything else after your rules gives you the homepage. Not so clever of me, an hour or two wasted on that!

asp.net site default document in subfolder

My default document is in subfolder not in root how can i make it default in asp.net 2.0 website.
Tried iis7 default document setting to '/pages/default.aspx'
'~/pages/default.aspx' but it didn't work.
Default document is not the same as start page. Default document means if I requested mysite.com/somefolder and didn't specify a file, which file should IIS display.
If you want to use a specific page as your home page, create a Default.aspx file and write this in it's codebehind class:
public override void ProcessRequest(HttpContext context) {
context.Response.Redirect("pages/default.aspx", true);
}
As the client might have disabled Javascript, a server side approach would be more reliable. However it's best to issue a permanent redirect instead of a simple Response.Redirect. Also doing it using JS will be bad from a SEO point of view.
You don't need to create a dummy Default.aspx page.
In your Global.asax.cs file, write the following:
public void Application_Start(object sender, EventArgs e)
{
var routeCollection = RouteTable.Routes;
routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx");
}
Explanation:
Application_Start code is guaranteed to run once and only once on the application start.
The first line of code, gets a collection of the URL routes for your application.
The second line of code, defines a new route pointing to your inner page in the subfolder that you wish.
The second argument is empty to indicate that this route is used when there's no specific page is requested and there's no Default document existing.
Default documents are a subfolder-specific thing - what you're trying to do won't (directly) work. Set up a default.htm file in the root, and have it refresh to your real "home page".
The better question you should be asking is how on Earth your homepage got out of the root directory.
In theory you could have a Web.config file inside the directory and use the defaultDocument element to set the default document. See here: https://stackoverflow.com/a/2012079/125938.
Unfortunately I haven't been able to get it to work myself locally, but that might be because it isn't supported in the Visual Studio development server.
Say "index.html" is the default page you want and it is present in "Public" subfolder.
Instead of specifying "/Public/index.html" as the default site, try "Public/index.html"

Resources