Why I cannot use HttpResponseMessage class? - c#

I'm new to ASP.NET Web API and want to make HttpResponseMessage instance from a utility class I made. Then I made very simple class.
But following compile error occurred.
CS0246: The type or namespace name 'HttpResponseMessage' could not be
found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace myapplication.App_Code.Utils
{
public class HttpUtility
{
// compile error here
public HttpResponseMessage GetHttpResponseMessage ()
{
return new HttpResponseMessage();
}
}
}
HttpResponseMessage is available from Controller Class which was made automatically by ASP.NET but not my Utility class.
What am I missing?

I have noticed that you placed your class in App_Code folder in your project. This folder has a special purpose in ASP.NET world and should be used for shared classes and business objects that you want to compile as part of your application. So either move your class into another folder, or change its Build Action in properties section to Compile.

Related

accessing files from a difference namespace c#

I have created a project structure like below
-Project.sln
--Provider.csproj
---Middleware
----Helper.cs
---Tests.csproj
----test1.cs
I am trying to access Helper.cs from test1.cs like using Provider.Middleware, but build is failing with message:
The type or namespace name 'Middleware' does not exist in the namespace 'Provider'.
Am I missing something?
Are you sure that you have Middleware as a folder in your project and something like this in your Helper class:
namespace Provider.Middleware{
…
}

Why I can't use my classes from Models folder in Global.asax.cs?

I'm trying to do following project for learning purposes:
https://learn.microsoft.com/ru-ru/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer
But I got stuck on the step "Updating the Global.asax file" - VS doesn't allow me to import WingtipToys.Models namespace to Global.asax.cs file.
Here's the project structure, Globa.asax.cs and 'using' error
Everything is done according to Microsoft's tutorial above.
Why can't I to import my models to global.asax.cs file?
My ProductDatabaseInitializer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WingtipToys.Models
{
public class ProductDatabaseInitializer : DropCreateDatabaseIfModelChanges<ProductContext>
{
protected override void Seed(ProductContext context)
{
GetCategories().ForEach(c => context.Categories.Add(c));
GetProducts().ForEach(p => context.Products.Add(p));
}....
What did really help is a re-creation of all classes in Models directory (delete all old classes and create new ones with same code) - then I started to see my classes from Global.asax.cs file.
Maybe it could help someone.

Models not recognized in namespace

Heey all,
I'm working on a Umbraco website. I try to reach my renderModels from my controller but, it says to me that type or namespace could not be found. So i try to add using namespace.Models;.
When i do this the namespace.controllers is reachable but when i try namespace.Models; it gives me the error the type or namespace name 'Models' does not exist in the namespace.
The folder Models does exist.
I tried searching online for it but could not find a awnser. Pls help
Edit 1
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using System.Globalization;
namespace MyNamespace.Models
{
public class HomeRenderModel : BaseRenderModel
{
public HomeRenderModel()
{
//
// TODO: Add constructor logic here
//
}
}
}
Controller
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using MyNamespace.Models;
namespace MyNamespace.Controllers
{
public class HomeController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var rendermodel = new HomeRenderModel(model.Content, model.CurrentCulture);
return CurrentTemplate(rendermodel);
}
}
}
Hope this helps, but basically I did the tutorial that mentions the model and controller etc. out there and had the same problem. I figured out that I had created the model folder containing the classes in the wrong place. You may have the same simple problem here.
Minimize all your folders in the project explorer, and then expand to see where you placed them. Your sub folders need to be in the project (in the 'folder' with the little globe icon, not above it with the solution properties.)

Cannot resolve dependencies or references in MVC3 C#

I have an error in MVC/Controllers/ProductCOntroller.cs when trying to build solution.
Need to have a reference to SportsStore.Domain in SportsStore.WebUI
using System.Linq;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
namespace SportsStore.WebUI.Controllers
{
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
repository = productRepository;
}
}
}
The type or namespace name 'Domain' does not exist in the namespace 'SportsStore' (are you missing an assembly reference?)
I have set up Project dependencies for MVC (WebUI) to C# class library (Domain)
How to fix it?
You need to add a reference to SportsStore.Domain in SportsStore.WebUI?.
You can do this be right clicking on the "References" folder under SportsStore.WebUI in the Solution Explorer and selecting "Add Reference...".
then navigate to the "Solution" Tab:
and select SportsStore.Domain.

'Using' not detecting namespace-checked client profiling already

I have a C# solution composed of three projects in Visual Sudio 2010. One is X.Domain (class library) the second, X.UnitTest and the third X.WebUI (MVC3). I was having problems with the using statement to reference the X.Domain namespace from withing the X.WebUI namespace. I thought it was a client profiling issue and verifired that all of the projects where using ".Net Framework 4" as the target framework. I checked to make sure the project dependencies for X.WebUI included the namesapce X.Domain. The exact errors I'm getting are:
The type or namespace name 'Domain' does not exist in the namespace'X' (are you missing an assembly reference). The error for the interface I'm trying to use from within X.Domain.Abstract: The type or namespace name 'IPersonRepository' could not be found (are you missing a using directive or an assembly reference)
In the X.Domain namespace/project, there are public methods, so I'm not sure why it's not exposed. I looked into these links here on stackoverflow, but they did not resolve the issue. Visual Studio 2010 suddenly can't see namespace?,
The type or namespace name could not be found ,
The type or namespace 'MyNamespace' does not exist etc
It's worth mentioning that someone posted that it could have something to do with a missed configured global.asax file, here
Type or namespace name does not exist
in that link, but I'm not sure about it. Could be the case, so I am posting my Global.asax file as well. I noticed the problem when I'm trying to create my 'PersonController' with ninject MVC3, I used Remo.gloo's blog to help me create the Global.asax file as posted here http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ and am following along with some additional code on p.164 of the the Pro ASP.NET MVC3 Framework book to create the controller.
PersonController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KLTMCInsight.Domain.Abstract;
namespace KLTMCInsight.WebUI.Controllers
{
public class PersonController : Controller
{
private readonly IPersonRepository repository;
public PersonController(IPersonRepository personRepository)
{
repository = personRepository;
}
}
}
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection;
using Ninject;
using Ninject.Modules;
using Ninject.Web.Mvc;
namespace KLTMCInsight.WebUI
{
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
If I missed something you need, I'll be more than glad to post it up. Again, Thank you for your help- Very appreciated.
Here is the solution:
I had used the 'Project Dependencies' box listed under the 'Project' file menu to select the dependencies of the X.WebUI project. Even though on in it I had checked the box for the X.WebUI project to depend on the X.Domain project... the reference was never created by VS2010. To solve the issue it was a simple fix by manually right-clicking the References folder under X.WebUI, and adding the reference to X.Domain through that window. Reference was added problem solved. Not sure why the reference was not made from the 'Project Dependencies' box in the first place, but if anyone comes across this same problem- this should remind you to check if the reference was actually made.

Categories