enter image description here
#pragma checksum "C:\Users\Nathan\Documents\ASPNET PJ\Views\Contato\Criar.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0419d52b5c6c647b8ba18e9d7de1b5045cdb5bb3"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.Views_Contato_Criar), #"mvc.1.0.view", #"/Views/Contato/Criar.cshtml")]
namespace AspNetCore
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using ContatoModel;
#nullable restore
#line 1 "C:\Users\Nathan\Documents\ASPNET PJ\Views\_ViewImports.cshtml"
using ASPNET_PJ;
#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "C:\Users\Nathan\Documents\ASPNET PJ\Views\_ViewImports.cshtml"
using ASPNET_PJ.Models;
#line default
#line hidden
#nullable disable
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(#"SHA1", #"0419d52b5c6c647b8ba18e9d7de1b5045cdb5bb3", #"/Views/Contato/Criar.cshtml")]
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(#"SHA1", #"30bddb7a998eb7023ecaaf3235c8530011feaaed", #"/Views/_ViewImports.cshtml")]
#nullable restore
public class Views_Contato_Criar : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<ContatoModel>
#nullable disable
Why am i getting these errors? I've already try to use "using Contatomodel;" but still dont working
To build and run without any of these errors
'using' is for namespaces, not classes. It is to tell EF where to find class references.
You are defining a Razor view typed for a ContatoModel...
public class Views_Contato_Criar : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<ContatoModel>
ASP.Net will likely not know where to locate this Model since it is most likely not in the same namespace (think Folder) as the view, so you need to use the using to tell it where to search for class references.
Go to your ContatoModel class file and you should see it is within a namespace scope. Replace the using ContatoModel; with a using and that namespace. For example, if we had a /Models folder in our application (called MyApplication) the default namespace would be like below:
namespace MyApplication.Models
{
public class ContantoModel
{
// ...
}
}
You would want to use:
using MyApplication.Models;
... in order for the View to resolve where to find the Model. You can usually figure out the namespace by looking at the folder structure, but that is just the default naming. As classes get moved around often their namespace doesn't reflect their new location so it's best to always check the class namespace itself.
This is fairly fundamental C# to understand so I would highly recommend reading and practicing on C# classes, namespaces, object references, etc. before diving head-long into things like Razor views where things can get a little fast and loose to bring C# into the land of HTML.
Related
When I'm using new class the Rider import the file by using the relative namespace, there is an option to set the Rider to use the absolute namespace instead?
for example, instead
using Collections.Generic;
autofill
using System.Collections.Generic;
If I understand you correctly - you need to check this official page.
In project settings you can set root namespace for all your project, and all your classes will import this namespace by pattern like:
{root namespace}.{local namespace (such as Contracts)}.{subnamespaces and etc.}
For example:
If you have Project called as Castle
and interface in local namespace IContract, and you want to use Logger namespace you will see smth like this
using Castle.Logger;
namespace Castle.Contracts;
public interface IContract {}
Is there a setting in Visual Studio (or ReSharper) that allows you to specify what namespaces should be default and which scope they are put in?
The default for an MVC project for example is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Namespace
{
public class Class1
{
}
}
but ReSharper and StyleCop complain:
All using directives must be placed inside of the namespace. [StyleCop Rule: SA1200]
Using directive is not required by the code and can be safely removed
Is there a way to make the default simply:
namespace Namespace
{
public class Class1
{
}
}
You can set this in Re-sharper.
Re-sharper > Options > C# > Namespace Imports > Add using directive to the deepest scope.
Update: As of VS2015 and Resharper10, this has moved. It is now under:
Code Editing > C# > Code Style > Reference qualification > Add 'using' directive to deepest scope
There is an easier way to do it nowadays without modifying templates and without ReSharper. Ever since Visual Studio 2019 update 16.1
was released you can setup the usings inside the namespace by following these steps:
Open Visual Studio
In the toolbar, go to Tools
Within the Tools menu, open Options
In the options list, select the Text Editor > C# > Code Style > General
Scroll down near the bottom, find the 'using' preferences section and set the Preferred 'using' directive placement to Inside namespace
Generally I don't believe there's any harm in including using statementents at the top of your class. I actually find it easier to include them there, so it's up to you whether you want to respect that rule.
If you do however, all of the file templates are available and can be edited. See the answer How do I edit the Visual Studio templates for new C# class/interface? to detail where they live on each Visual Studio version.
Once you're there you can change the layout, so for example a basic class looks like:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
You could change this to the following or similar:
namespace $rootnamespace$
{
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
class $safeitemrootname$
{
}
}
There may be quite a few files to change though!
There's also a Visual Studio extension for VS 2015 and 2017 that fixes using declarations after the fact. It also has some configuration options for grouping particular declarations (like System.*)
https://marketplace.visualstudio.com/items?itemName=everfor88.UsingDirectiveFormatter
In ReSharper 2020, it's under Code Editing > C# > Syntax Style > Add 'using' directive to deepest scope.
Using .editorConfig you can add the following.
# 'using' directive preferences
csharp_using_directive_placement =inside_namespace:warning
I'm implementing a number of micrososervices with C# and Service Fabric. All has gone well until this error which seems to be a C# error:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'IProductCatalogService' could not be found (are you missing a using directive or an assembly reference?) ECommerce.API C:\dev\ECommerce\ECommerce.API\Controllers\ProductsController.cs 14 Active
IProductCatalogService is the project ECommerce.ProductCatalog.Model which I have set a project dependency to and I am referencing with a using statement:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECommerce.API.Model;
using Microsoft.AspNetCore.Mvc;
namespace ECommerce.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly IProductCatalogService _catalogService;
Here it is ECommerce.ProductCatalog.Model:
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.ServiceFabric.Services.Remoting;
namespace ECommerce.ProductCatalog.Model
{
public interface IProductCatalogService : IService
{
Task<IEnumerable<Product>> GetAllProducts();
Task AddProduct(Product product);
}
}
What am I missing?
From the screenshots above, it looks like you're missing out on referencing to the right project. You want to use using Ecommerce.Productcatalog.Model after you add the reference of Ecommerce.Productcatalog.Model in UI Project.
And from your comments it looks like you have done that and yet you're not able to add that reference. In that case, can you share the screenshot of what it says when you try adding that using statement and also share the project/folder structure if possible, so we can take a look it further.
I am using Visual Studio 2013. I am trying to start unit testing by using this tutorial.
I have added a class library and a reference to MVC. However, the Intellisense/Autocompletion is not working properly within my class library. Currently, this is all the code I have in my test class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Web.Mvc;
using System.Web;
using Application_Portal;
using Application_Portal.Controllers;
namespace ApplicationPortalTests
{
[TestFixture]
public class HomeControllerTest
{
HomeController home = new HomeController();
}
}
The Intellisense does not seem to recognize the home variable (no suggested properties, etc.) Typing home.Index() gives the following error:
ApplicationPortalTests.HomeControllerTest.home' is a 'field' but is used like a 'type'
Furthermore, it does not even seem to recognize "var" (as in var result = ...). Instead, when I type var and hit space, it autocompletes it as EnvironmentVariableTarget.
I have tried cleaning and rebuilding the project, and closing and reopening Visual Studio, but with no success.
What might the issue be? I appreciate any advice.
You have declared your variable inside the class. If you want to use this variable, it must be within the context of a member. For instance:
[Test]
public void test_my_index_page()
{
var result = home.index();
}
I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).