Reasons why WebResources cannot be found - asp.net

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!

Related

Routing for Single Page Application in ASP.NET Core

I have a Single Page Application written in JavaScript, and I use HTML5 history API to handle URLs on the client side. This means any URLs sent to the server should cause the server to render the same page.
In the ASP.NET MVC 5 I wrote this code to do this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.Add(new Route("{*path}", new MyRouteHandler()));
}
}
public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return WebPageHttpHandler.CreateFromVirtualPath("~/index.cshtml");
}
}
This worked really well. No matter what URL the server gets, it renders index.cshtml. Note that I am able to use a .cshtml file (as opposed to an .html file) which means I can have some C# code to dynamically change what .js scripts are included, append version numbers to .css files, and so on. What's more, I didn't have to implement controllers and views and so on just to render this .cshtml file.
Now we come to the question: How do you do this in ASP.NET Core? I have been reading the documentation, but I don't see how to render a .cshtml file without adding controller classes, view folders and other rigmarole.
Anyone know the equivalent code in ASP.NET Core?
Currently to run a CSHTML page "the normal way" in ASP.NET Core requires using ASP.NET Core MVC.
However, there is a planned feature that is being worked on that is somewhat similar to ASP.NET (non-Core) Web Pages, where you can have standalone CSHTML files. That feature is being tracked here: https://github.com/aspnet/Mvc/issues/494 (and as far as naming for the new feature, that is being tracked here: https://github.com/aspnet/Mvc/issues/5208).
There's also a sample of how to render an MVC view to a string (e.g. to generate an email, report, etc.), and that sample is available here: https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.RenderViewToString
But to use this sample in the scenario you describe, you'd have to do some extra plumbing to wire it up as its own middleware (not a lot of plumbing; just a little!).
It's also worth noting that in your scenario you probably don't want all URLs going to this one view, because you still need the static files middleware running first to handle the CSS, JS, images, and other static content. Presumably you just want all other URLs to go to this dynamic view.

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.

How can I remove ASP.NET Designer.cs files?

I've worked on VS projects before where there is no .designer.cs files.
Now I started a new project on a different computer and I can't get rid of designer.cs files. It's really annoying me. Do I really need it, how can I remove it? There must be a setting somewhere.
YES! You can remove them......here is how.....
HOW TO DELETE DESIGNER.CS PAGES FROM YOUR WEB APPLICATION
After great torture and much testing seeking a way to avoid designer.cs pages in Visual Studio (v. 2015 and earlier), I finally found a work around for this. If anyone is stuck with designer.cs pages in a Web Application Project in Visual Studio this solution will allow you to erase all application compile errors quickly then delete the designer.cs pages completely from your project.
First understand the following:
Web Application Projects use designer.cs pages (partial classes) auto-generated by Visual Studio, and which are tied to the design tools built into Web Applications which the web app model sustains. I call it web sites for dummies. I could find no setting or way to turn the creation of partial classes and designer.cs pages off, as they are often unnecessary code tied more to the IDE than the functioning of the application. All partial classes get compiled into one class anyway. Web Apps also get pre-compiled or built ahead of time in general by design, and those dll's gets pushed into bin folders.
Web Site Projects do not use designer.cs files and are less tied to the IDE. They allow for a cleaner coding of class structures. They also use partial classes. But Web Sites Projects generally get compiled at runtime.
SOLUTION - How to Remove "...designer.cs" errors and files from a Web Application Project in Visual Studio.
Unless you want to convert your web project from an Application to a Web Site model in Visual Studio, this solution below allows you to run your project as is, yet like a Web Site, where you can move most or all the designer.cs partial class control references from that file into your main web page partial class file. It also removes all errors AND does not interfere with the Visual Studio recreating those designer files should other developers be sharing them and adding controls to pages and forget to use this solution on specific pages.
In the front-end .aspx pages, in the #PAGE directive at the top, change the "CodeBehind" to "CodeFile". Make sure it still references the same .cs code or class file.
Add the "CodeFileBaseClass" attribute to the same #Page directive in your web page and have it access the fully qualified path to the same .cs above with any namespaces in the path.
Make sure you use the "Inherit" attribute with the same path as the "CodeFileBaseClass".
You should have the following for every web page in your Web Application with these attributes formatted as such:
<%# Page Language="c#" CodeFile="index.aspx.cs" CodeFileBaseClass="YourNameSpace.index"
Inherits="YourNameSpace.index" %>
Now go into your designer.cs file for the page and copy any control references from the partial class in your designer file into the partial class of your main .cs file for the web page. If you have lost your designer.cs files, just add in any web controls as field references as fields that the compiler says are missing. After you do that DELETE the designer.cs file. You don't need them and have complete control over your web page controls using the main .cs file and its partial class.
Below is what I had in the designer.cs file before deleting it. Below it is the main index.aspx.cs file for my project after I added the designers.cs control reference as a field in its partial class. I then deleted the designer.cs file and its code completely from my project:
namespace YourNameSpace
{
public partial class WebForm1
{
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.Literal Message1;
}
}
In my index.aspx.cs file below you can see where I pasted the last "Message1" reference line from the designer above into its partial class at the top. You can see how I was then able to access the web page Literal control, Message1, in my Page_Load event and modify the text. That shows it was referenced correctly now, compiled, and worked, where before such references failed if the designer.cs file was missing or its partial class failed:
namespace YourNameSpace
{
public partial class index : System.Web.UI.Page
{
protected global::System.Web.UI.WebControls.Literal Message1;
protected void Page_Load(object sender, EventArgs e)
{
this.Message1.Text = "hello world";
}
}
}
The key to fixing this is the CodeFileBaseClass and CodeFile attributes in the #Page directive in the main web page. This prevents new member references and controls in the web page from being automatically generated by the Visual Studio IE and stuck as stubs in designer page partial classes (which to me is not helpful when you want full control over those fields in a single convenient area).
Note: If you later regenerate the designer.cs files again for your project by selecting your project in VS, then in the top menu choosing Project > "Convert to web Application" those designer.cs files will get recreated, BUT Visual Studio will ONLY place references to missing web controls in them that are NOT now added to your main .cs partial class page. In other words, any references I copied and added above to my index.aspx.cs partial class file would not be recreated in the new designer.cs file. So there is no conflict with the Web Application model you are using. When you see those designer files, you can now have developers safely copy them into your main code behind .cs files and safely delete the designers if you like.
You're dealing with a web application rather than a website (clarification)
Yes, in the context of a web application, you do need them.
The real "answer" is for MS to simply allow you to not use them.

Cannot Parse .aspx during WAP Conversion

I've copied over the Web Site files to a new WAP project. Made sure that it compiles and it does. The problem I have is that it can't parse the .aspx pages when I perform a convert to WAP. The error simply says it can't parse the .aspx pages.
When I look at one of the .aspx pages, I have the page directive as such but it can't recognize that namespace. That is the namespace I'm using for my new WAP project.
My code-behind pages all have the namespace Instant.Web as well as the project properties is set to Instant.Web for the primary namespace.
so when I go to a page for example, it's not recognizing the Web.Attachment portion of my Inherits.
Here's an example (slightly modified the wording for privacy):
SomeAspxPage.aspx:
<%# Page Language="C#" Inherits="Instant.Web.Attachment" Codebehind="SomeAspxPage.aspx.cs" %>
<asp:Content ID="ctlContent" ContentPlaceHolderID="ctlContentPlaceHolder" runat="Server">
<Frm:Attachment ID="Attachment1" runat="server"/>
</asp:Content>
SomeaspxPage.aspx.cs
namespace Instant.Web
{
public partial class Attachment : Instant.InstantFrm.Pages.Base.UserPage
{
}
}
Also for some reason UserPage in the code-behind isn't showing as a class that you can right-click and go to source. Not sure why because that assembly is definitely in this WAP Project and it's the latest assembly for Instant.InstantFrm
Do not know if it is relevant for your real case, but as for your example, you have an attribute Codebehind="Attachment.aspx.cs" which looks for the file Attachment.aspx.cs, but the file's name is SomeaspxPage.aspx.cs. Usually "file could not be parsed" means the wizard cannot find the matching code-behind file and cannot convert the page.
BTW one more note from here:
Before converting the project files, add references to the Web
application project for assemblies that existed in the Bin folder to
help prevent errors.
It is not clear from your description if you have added the reference to your custom UserPage. If not and that assembly is just copied somewhere into WAP Project it can also cause the converting error.
Try adding a new blank page to your project, check if that can be parsed.
Then in small steps add functionality until you get the page that is the same as the one that is giving you the error.
This should allow you to identify what the problem is.
Based on this quote Warning 126 Unrecognized tag prefix or device filter 'asp' in one of the comments, it sounds like you are missing an Assembly reference in your CSProj file.
Verify that you have System.Web in your References folder of the project.
If it is there, verify the version (directory) it is pulling from is the same as what .NET version you are targeting with your project.
Save and close the solution, then re-open it and try again.
I'm theorizing that VS is caching something and the projects needs to be reloaded.
For WAP site, I'd remove the ASPX layout completely (except the first line, which is required) and output the content using Response.Write in codebehind.
In addition to simplicity, this gives much cleaner output and granular control over the process.

How to add a reference to my Web Service Proxy from a Custom Class

I'm creating a custom class to abstract out some of the repeated SOAP header work. I want to reference a Web Service Reference I just created in my custom class so I can create an instance of it. How do I reference it?
Notice I said I am trying to reference a Web Service "reference" (right click in VS and I added a "Web Service Reference" not a "Web Service"). So I'm trying to create an instance of that Proxy class that was created in MyCustomClass.cs
Once you have the reference created you need to add an import(vb) or using(c#) statement in the code file you want to use it. After that you simply need to instantiate an instance of the web service class.
// add the service reference
using ServiceReference1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create the instance of the web sevice of the class
SomeWebService sweb = new SomeWebService();
//call the web services HelloWorld Method
sweb.HelloWorld();
}
}
Hopefully this was what you were asking for
This is more Web Site weirdness. I recommend that web sites be used only for pages, images, css, js, etc, Anything else should be done in a separate project, and the web site can reference the other project.
I avoid web sites like the plague, so I've never had to make this work, but consider that web sites don't build. Instead, various things are built on the fly, when the site is used. There will be no Reference.cs file in a web site.
In more recent versions of Visual Studio the using statement for these web service references also has had to include the project name.
Using cptScarlet's original code example, change the first line to look like this:
// add the service reference
using MyProject.ServiceReference1;
When you type in your project name, the class and/or namespace of the objects created in the web reference should show up in the intellisense.

Resources