Allowanonymous attribute does not work in asp.net mvc - c#

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.

Related

C# Response.Write Error from scaffolded "dotnet new mvc"

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).

how to decorate action in web api with ValidateInput(false)

I am in need of migrating from normal MVC controller to WebApi controller.
My normal mvc controller.
[HttpPost]
[ValidateInput(false)]
public ActionResult Validate(string data)
{
}
WebApi Controller
[HttpPost]
[ValidateInput(false)]
// the moment I resolve the namespace System.Web.Mvc all the [HttpPost],[HttpGet] starts throwing error.
public HttpStatusCode Post([FromBody]string data)
{
}
Can somebody explain why so & how to address this requirement?
To have an Api Controller, you'll have to inherit from ApiController. Also, if by adding the Namespace you start getting errors, then you will have to add the namespace explicitly to the attributes like this for example. System.Web.Http.HttpPost.
You don't really have to use something like [ValidateInput(false)] in your web api, it's meant to prevent XSS attacks in MVC applications.
Also, the namespace you have to refer is System.Web.Http.
I used htmlEncode in javascript from here and HttpUtility.HtmlDecode in C#

Getting error on Route attribute in Api method

I have created Api controller and trying to add 1 api method but getting error when adding Route attribute on my api method:
This is my method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
public class MyController : ApiController
{
[AllowAnonymous]
[HttpPost]
[Route("api/abc/Get")] //Getting error :type or namespace Route could not be found
public async Task<object> Get()
{
}
}
My dll references:
But on Route Attribute i am getting error:
Error:Type or namespace Route could not be found
Probably you need to check if you have System.Web.Http assembly in your project references. if you used nuget please check if you have web api packages installed
you can check it here:

Upgraded from Umbraco v4 to v7 SurfaceController No route in the route table matches the supplied values

I recently converted our intranet Umbraco site from v4 to v7.2 and also converted all the webform masterpages to mvc.
I am trying to convert a usercontrol that should be a child action to a SurfaceController but I am getting the dreaded "No route in the route table matches the supplied values" error when trying to call the action:
#Html.Action("ServiceStatusInfo", "ServiceStatusSurface")
This is just a get action that doesn't require a view or a model. It just calls the action on the server and the server updates a file on the server that then get's read by some javascript.
I have done a lot of searching and I created a sample solution using Umbraco 7 and created a controllers folder, then a "MySurfaceController" and I was able to call the action from the masterpage of the sample solution with no issues but in the recently converted project it seems like there is some weird routing issue going on. I compared the web.config's for both the current project and the sample one and they pretty much have the same entries (I thought maybe I missed something). It seems that my converted project is not recognizing the routing. Any help will be appreciated.
Here is the SurfaceController
using Umbraco.Web.Mvc;
using System.Web.Mvc;
namespace MyUmbracoApp.Controllers
{
public class ServiceStatusSurfaceController : SurfaceController
{
// can't reach this either:
public ActionResult Index()
{
return Content("hello world");
}
// this is what I am trying to reach
[ChildActionOnly]
public ActionResult ServiceStatusInfo()
{
// do some stuff to get the status
return CurrentUmbracoPage();
}
}
}
I have also tried using the "PluginController" option even though this is not a plugin with the "area" attribute but same problem.
Maybe there is a workaround that I am not aware of ?
Change StatusInfo to ServiceStatusInfo in your action call. This should match the name of the action.
#Html.Action("ServiceStatusInfo", "ServiceStatusSurface")

HttpApplicationState not available in an MVC controller

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?

Categories

Resources