I have a virtually empty asax file that has just one line of code:
<%# Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Applications.Global" %>
Shouldn't there be more accompanying code?
Applications.Global is defined in the code-behing like this:
namespace Applications
{
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
//Helper.CacheMasterTemplate();
}
}
}
but the compiler complains that it cannot load type 'Applications.Global' from the first line of the asax file. How can I change the code to make this work?
You need to fix the problem. Most often, you get that error because the output path has been changed. It should be bin. If you target x86, Visual Studio changes it to bin\x86\Debug. Change it back. Go to PROJECT > YourProject Properties > Build and check the Output Path.
Related
I have an aspx page using only inline C# without a codebehind.
Running from Visual Studio it works, however adding the aspx file to the wwwroot of my local machine gives the message
Compiler Error Message: CS1513: } expected
If I rearrange the code to have the method above the code the message is instead
CS1519: Invalid token 'try' in class, struct, or interface member declaration
I expect it is something to do with server configuration, though I don't really know where to start.
The issue only occurs if there is methods, removing method1 from the below results in correct running.
This is the minimum entire file I was able to cause the error with
html
<%# Page Language="C#" %>
<%
try
{
string message = "Text";
Response.Write(method1(message));
}
catch
{
Response.Write("Err");
}
string method1(string source)
{
return source;
}
%>
If you're embedding code directly on the aspx page, that code still has to go in a method. Ex:
<%# Page Language="C#" %>
<%
void Page_Load(Object sender, EventArgs e)
{
try
{
string message = "Text";
Response.Write(method1(message));
}
catch
{
Response.Write("Err");
}
}
string method1(string source)
{
return source;
}
%>
By the way - we don't typically put C# code directly in the .aspx file. That's what the code behind system is for.
And we don't typically write directly to the response. It's hard to control where the markup will end up. In Web Forms, it's better to declare a control on the page and then add markup to that (or show/hide controls). Of course if we're going to talk about doing things well - then don't use Web Forms in the first place. It's a dead technology.
I have messed around with the Visual Studio project, running on an earlier version of .NET/C# yields the following error, It seems possible that this is the actual problem I have had
Error CS8059 Feature 'local functions' is not available in C# 6. Please use language version 7.0 or greater.
I am unsure why it thinks method1 is a local function and details are hard to come by.
Edit: To solve my problem I put the method in script tags instead of embedded code blocks. The methods then could be used from the embedded blocks and the script tag.
<script runat="server">
string method1(string source)
{
return source;
}
<script>
I am making a simple api without any ide. I have three files in my website's root.
default.aspx
<%# Page Language="C#" src="default.cs" inherits="foo" %>
default.cs
using System;
using System.Web;
using System.Web.UI;
using TechStreet;
using MySql.Data.MySqlClient;
public class foo : Page {
protected void Page_Load(object sender, EventArgs e) {
Response.Write(Database.Query("SELECT * FROM users", new String[0]));
}
}
Database.class.cs
namespace TechStreet {
public class Database {
public static string Query(string query, string[] parameters) {
return "done";
}
}
}
My problem is that loading default.aspx, it rightfully doesn't recognize the TechStreet namespace because I never tell them where the file is. If I understand this correctly then I need to compile both these files together or provide a reference to the database.dll when I compile default.cs. But I never compile default.cs. It gets compiled automatically when the page is first loaded.
Another solution is to compile the database.cs and put it in /bin/. But I will have to do this every time I make a change in the file.
So my question is, is there any way to "tell" default.cs to also auto compile and reference the database.cs when it compiles itself?
Move the Database.class.cs into the App_Code directory.
I am attempting to use the Global.asax file and it is failing miserably. Every line in the code has an error but the funny thing is that it does not matter how many lines, what the lines contain, etc have as it always reports an error. This makes me thing there is a configuration setting that isn't right but wouldn't know where to begin.
The project is a Web Site rather than a Web Application but I read up on Global.asax and it appears to be acceptable to use it in a Web Site. I have provided the code I am using to test that the Global.asax works in the project before writing the code needed for the project.
<%# Application Language="C#" %>
<script runat="server">
static string _pagePath;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
_pagePath = Server.MapPath("~/Folder/Page.aspx");
}
// ...
void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.PhysicalPath;
if (path == _pagePath)
{
Response.Write("Page viewed");
Response.End();
}
}
</script>
I have also provided a list of example error messages that are cropping up
'Class' statement must end with a matching
'End Class' 'If', 'ElseIf', 'Else', 'End If', 'Const', or 'Region' expected
'Namespace' statement must end with a matching 'End Namespace'
Statement cannot appear outside of a method body/multiline lambda.
Hopefully someone can pick out what I did wrong so I can use the Global.asax. Any help is widely appreciated!
add Language="C#" property to the directive at the top of your global.asax. right click on the file, choose view markup, add the property and rebuild.
<%# Application Codebehind="Global.asax.cs" Inherits="GLOBAL CLASS" Language="C#" %>
I have a VS solution set up using build scripts to copy the compiled DLL into another project. The base project builds fine, and copies correctly to the target.
However, the target project won't compile as it can not find a particular class within the namespace:
foo.cs
namespace foo {
public class bar {
public static string myVar {
get { return "A string"; }
}
}
}
myPage.aspx.cs
using foo;
namespace foo.foo2 {
partial class bar2 {
protected void Page_Load(object sender, EventArgs e) {
// can access foo.bar here in the source project but not once the DLL is compiled and copied to target
var myVar = bar.myVar; // The name 'bar' does not exist in the current context
}
}
}
Why would this compile correctly in the source project, but prevent the target from building?
EDIT: Second project builds fine if I exclude myPage.aspx from the project. But I shouldn't have to do that.
You have very likely an incorrect assembly referenced.
Make sure you use the correct physical assembly. Sometimes, a dead (old) version lies around (such as in the GAC) and this one is referenced rather than the believed new version.
Easiest way to confirm is to rename the assembly file to something else and reference the newly named assembly. bar.myVar should show up immediately.
I am working with a set of T4 script that generate partial classes for my entities in a EF application.
Because partial classes need to reside in the same assembly, the script resides in the same project as the entity classes. It also needs access to the compiled assembly when executing.
When an error occured, the script will fail with the output "ErrorGeneratingOutput". This will cause the whole project to NOT compile, because the generated file is an .cs file, with (at that point of time) invalid content.
So thats a vicious dependency circle, which can only be broken if I manually remove the error message from the generated file, and then trigger the build.
If there was a way to suppress the error message (or replace it by an empty string), my life would be much easier.
So the question is: can i change the error handling of a t4 script?
In some cases, this problem can be solved easily.
In my case, the problem was about loading the DLL of the project the T4 script resides in. The assembly directive was placed in the top region of the script (line 5). So i changed the output extension to txt.
<## template language="C#" hostspecific="True" debug="True" #>
<## output extension="txt" #>
<##assembly name="invalidAssemblyName"#>
Then I placed the real output into another file using the EntityFrameworkFileManager.
<## include file="EF.Utility.CS.ttinclude"#>
<#
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
fileManager.StartHeader();
fileManager.StartNewFile("Output.cs");
#>
//content
<#
fileManager.Process();
#>
When the error occured, that an assembly cannot be loaded, the ErrorGeneratingOutput message is printed to the default .txt file, where it does not produce a problem for the compilation. If the assembly can be loaded, the output is printed to the Output.cs file.
This way the project can be build after repairing the initial problem, and the developer doesnt have to take care about the ErrorGeneratingOutput problem, too.
I don't know if it is possible, BUT,
you CAN put the T4 scripts in a seperated project and use an MSBuild Task to copy your generated files to your EF entities project.
Your solution should contain
Your EF entities project, let's call it Entities
An entity generator project (you'll put your T4 scripts here), call it EntitiesGenerator for example
You also need to create a project for the custom MSBuild Task which would copy your generated C# files to your "Entities" project
To do so, create a class library project, MyBuildProcess
Reference the following assembly :
Microsoft.Build.Framework (located in C:\Windows\Microsoft.NET\Framework\v4.0.30319)
Now, let's write the custom task
Add a class file to your project, CopyGeneratedEntities.cs, for example
using System;
using Microsoft.Build.Framework;
using System.IO;
namespace MyBuildProcess
{
public class CopyGeneratedEntities : ITask
{
private IBuildEngine _buildEngine;
public IBuildEngine BuildEngine
{
get { return _buildEngine; }
set { _buildEngine = value; }
}
private ITaskHost _hostObject;
public ITaskHost HostObject
{
get { return _hostObject; }
set { _hostObject = value; }
}
public bool Execute()
{
// Copy generated Product entity to EF project
if (File.Exists(#"C:\MySolution\EntitiesGenerator\ProductEntity.cs"))
{
File.Copy(#"C:\MySolution\EntitiesGenerator\ProductEntity.cs",
#"C:\MySolution\Entities\ProductEntity.cs", true);
}
return true;
}
}
}
Build your project
Now edit the .csproj file correponding to your T4 project (EntitiesGenerator) and reference the custom task by adding the following just under the <Project ... > tag :
<UsingTask AssemblyFile="C:\MySolution\Libs\MyBuildProcess.dll"
TaskName="MyBuildProcess.CopyGeneratedEntities" />
And call the task like this (at the end of the csproj file, before the </Project>) :
<Target Name="AfterBuild">`
<CopyGeneratedEntities />
</Target>
Now, when you build the EntitiesGenerator project,
T4 renders your entities and, once the build is over, your custom task is called and your files are copied to your "Entities" project.
You'll only need to manually reference the generated C# files to your Entities project after the first generation,
then they simply be overwriten.
For more information about MSBuild
see.
MSBuild Team Blog - How To: Implementing Custom Tasks
Microsoft.Build Namespaces