I am getting this error "the type or namespace name 'Umbraco' could not be found"
I am using it in a custom controller HomeController.cs which lives in root/controllers/HomeController.cs.
But I can not seem to find any Umbraco namespaces.
using Umbraco.Web;
using Umbraco.Web.Models;
namespace umbraco.umbraco.Controllers
{
public partial class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
}
}
any help with the basic is greatly appreciated
Regards
Can you try renaming your namespace?
umbraco.umbraco.Controllers
to
YourProject.Controllers
Related
I'm currently playing around with SourceGenerators and want to create a controller for each model of an existing DBContext.
I kickstarted the project with Scaffold-DbContext DemoDatabase and added the Person controller and the API is working fine. The project is in net6.0 and the generator is in netstandard2.0.
My handwritten Controller in the API Project looks exactly the same as the generated Code.
I managed to create the following file via the SourceGenerator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using DemoApi.Models;
namespace DemoApi.Controllers.v3
{
public partial class PersonController : ODataController
{
private ModelContext _context;
public PersonController(ModelContext context)
{
_context = context;
}
[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_context.Person.ToList());
}
}
}
However VisualStudio outputs an Error stating that the ModelContext does not contain a definition for Person. Either the syntax highlighting is not working properly or the I'm missing something because I can't navigate the generated files (eg. pressing F12 on ODataController just outputs: Cannot navigate to the symbol under the caret.)
Are there any possible way for the generated files to recognize the existing ModelContext because referncing is via using DemoApi.Models did not work or am I missing something?
Any help would be greatly appreciated
Thanks in advance
I'm familiar with PHP and JS, as well as MVC methodology, but I'm completely new to C# and have spent time looking for the documentation on this specific error.
I used dotnet new mvc to create a working app on port 5000. Also note, I am working in the Controller, not the model or view:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ExelonPrime.Models;
namespace OptimusPrime.Controllers{
public class ApiController : Controller
{
public void Help_Pdf()
{
Response.Write("test");
}
}
}
And the error I get (when trying to compile) is:
error CS1061: 'HttpResponse' does not contain a definition for 'Write' and no accessible extension method 'Write' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?)
If I'm missing a using directive, which one is it? I tried System.Web and that didn't work. How do I make this work?
I would recommend following through microsofts tutorial on using asp.net core.
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-2.2
As far as this specific instance, rather than using Response.Write, I would do this
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SampleWeb.Models;
namespace SampleWeb.Controllers
{
public class ApiController : Controller
{
[HttpGet]
public ActionResult<string> Help_Pdf()
{
return "test";
}
}
}
This specific sample might be helpful.
With this class, the url https://localhost:5001/api/Help_Pdf returns "test"
In ASPNET Core, when writing an API, it's more common to return an object, or POCO in stead of directly writing to the response stream (although it indeed is possible)
try changing your
public void Help_Pdf()
{
Response.Write("test");
}
to
[HttpGet()]
public IActionResult Help_Pdf()
{
return Ok();
}
this will return an 204 (no content), you can pass data however, to the OK function, to add a body to the response object.
If your trying to write directly to the response stream like that, you can do this:
public async Task Help_Pdf()
{
await Response.WriteAsync("Test");
}
However, I'd recommend not doing this in your actual app, the method should return an IActionResult. IMO it's not good practice to write directly to the stream in your controller methods (not very test friendly).
I am building a web service using web API controller. However, when I try to decorate any of my actions with [allowanonymous] attribute it doesn't work... here is an image
I just would like to know what is goning wrong in here...
Just fully qualify the name
[System.Web.Mvc.AllowAnonymous]
public ActionResult SomeAction()
{ ...
On the top of the page, use namespace like:
using System.Web.Mvc;
Just had to remove this using:
using System.Web.Http;
Because that namespace is for WebAPI, whereas System.Web.Mvc is for MVC Controllers. Using both namespaces in the same file may cause confusions for Authorize and AllowAnonymous, for example.
I am trying to use Mike Wallace's "RequireHttps" attribute to force certain controllers to require SSL or not.
I have the code below, and it builds fine. But when I actually go to add the attribute to the controller, it fails.
I have other custom attributes that appear, and I have another that doesn't. So it might be a project issue, though I tried a new project and it still failed. The code is in the app_code folder.
using System;
using System.Net.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
namespace WebAPIService
{
public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
public RequireHttpsAttribute();
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
{
ReasonPhrase = "HTTPS Required"
};
}
else
{
base.OnAuthorization(actionContext);
}
}
}
}
Any suggestions are welcomed. Thanks.
EDIT:
The error I get on the API Controller is:
The type or namespace name 'RequireHttpsAttribute' could not be found (are you missing a using directive or an assembly reference?)
You can just put your RequireHttpsAttribute.cs file in a different folder than App_Code, which is a special ASP.NET folder. Your namespace import should function correctly, then. If your project is a website project, converting it to a web app might also solve the problem.
If you want to learn more about the issue, I recommend you take a look at Type or namespace could not be found from App_code folder.
I'm using MVC2 and VS2010 developing a website and need to use Application State global values. I can set a value like 'Application["hits"]=0;' in Global.asax but when trying to use the same in an MVC controller always get the following error:
The name 'Application' does not exist in the current context
I have also tried using in the Global.asax in order to define a global variable but it triggers the following error:
A namespace cannot directly contain members such as fields or methods
I'm looking for a way to define global Application State values that are available within all controllers of my MVC2 web application. Am I omitting something? My controller looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCApplication.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
Application["hits"] += 1;
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
}
}
I appreciate any solutions and/or suggestions.
Thanks
Mehrdad
I think that in MVC3 you can get access to an actual HttpApplicationState object via the
HttpContext.ApplicationInstance
property. That is:
HttpApplicationState application = HttpContext.ApplicationInstance.Application
In ASP.NET MVC2, i use
HttpContext.Application["foo"] = "bar";
and to get
HttpContext.Application["foo"]
You could use a static class with an internal dictionary and an indexer.
Also, have you tried HttpContext.Current.Application?