How to override an Action method in Mvc - c#

I'm using a Cms for Mvc. This Cms has the following Controller:
public class OrderController : Controller
{
public ActionResult Index()
{
return View();
}
}
For customization needs, I'd like to override the behaviour of this controller and return something different when the same URL is visited by the user. What's the best approach in order to achieve this result?
I tried to inherit the Cms Controller and make the ActionResult an override, following this answer: How to override controller actionresult method in mvc3?
public class OrderController : Cms.Areas.Admin.Controllers.OrderController
{
public override ActionResult Index(Guid orderItemId)
{
// Do extra stuff
return View();
}
}
But this doesn't work. When I try to navigate "admin/order" I still enter in the Cms Controller/Action.
Any suggestion?
NOTE: The Controller I'm trying to override is in another assembly and the action is set to virtual. It's in an Area, therefore the Route is configured inside AreaRegistration.

Your request need to use OrderController instead EcommerceOrderController, take a look on your MVC routes

This seems to me to be a routing question. It doesn't matter if you override the controller if your route still points to the original. If you want a URL to invoke your action, you need to add a route with a higher priority than the one that is currently resolving to the original.

Related

How can I make Url.Action() recognize areas defined in referenced Razor Class Libraries?

In my ASP.NET Core project's Razor, I'm using Url.Action() like this:
#Url.Action("Index", "Foo", new {Area = "MyArea"})
The controller action is defined in a referenced Razor Class Library like so:
[Route("MyArea/Foo")]
[Authorize]
public class FooController : Controller
{
[HttpPost]
public IActionResult Index()
{
return Ok();
}
}
Back in my ASP.NET Core project, in the Visual Studio code editor, MyArea appears in red, and the hover tooltip states "Cannot resolve area 'MyArea'". And of course, my call to Url.Action() returns string.Empty....
But the route is valid.
What change(s) could I apply to either my ASP.NET Core project or the referenced RCL to cause MyArea to be recognized as a valid area, and make the call Url.Action() return the expected URL?
[Area("MyArea")]
[Authorize]
public class FooController : Controller
{
[HttpPost]
public IActionResult Index()
{
return Ok();
}
}
Areas are different part of the route since they are named.. So you have to use the [Area()] otherwise it doesn't know what you are asking for the route. By convention the Foo route is already know since it's the name of the controller. This assumes you have the route mapped correctly in the Configure() method in your startup.cs. This also assumes that you are using the Area folder with a folder called MyArea in it.
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-3.1

Questions about routing in Asp.net Core MVC 2

I can't wrap my mind around the routing mechanism in asp.net core MVC 2.
Here's what I have:
I already built a functioning page to add 'Materials' to a 'Application'.
The URL to call this page is:
http://localhost:33333/AddMaterial/Index/57
which uses the default route:
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
Whereby 57 is the application id, so that I know what 'Application' gets the new 'Material'. The Index Method in the controller looks like this, and works like expected:
[HttpGet] public IActionResult Index(string id)
{
var model = GetModel(context, int.Parse(id));
return View("Index", model);
}
So far so good... Now here's my problem:
I want to use the same controller and view to also edit 'Materials'. But for that i'd need two parameters in Index(). I'd need:
string applicationId
string materialId
as parameters.
So I added a new route:
routes.MapRoute(
name: "whatImTryingToAchieve",
template: "{controller}/{action}/{applicationId?}/{materialId?}"
);
And of course I updated the controller:
public IActionResult Index(string applicationiId, string materialId)
{
// Do stuff with materialId etc.
// ...
return View("Index", model);
}
I know that the routing system has a specific order. So I tried defining my new route before the default route. That didn't work.
I then tried to put it after the default route, which didn't work either.
I read through a lot of information about the routing system, but I didn't seem to find the answer to my simple question:
How can I add another, specific route?
Help would be much appreciated :)
EDIT: Attribute based routing as suggested by Igors Ĺ utovs
Controller:
[Authorize(Roles = "Admins")]
[Route("AddMaterial")]
public class AddMaterialController : Controller
{
//....
[Route("AddMaterial/{applicationId}/{otherid}")] // Nope. Nothing.
[HttpGet] public IActionResult Index(string applicationId, string otherid)
{
return View();
}
[Route("AddMaterial/Index/{applicationId}")] // Didn't work.
[Route("AddMaterial/{applicationId}")] // Didn't work either...
[HttpGet] public IActionResult Index(string applicationId)
{
return View();
}
}
So much for Attribute base routing.
I will provide my subjective opinion on the essence of the described problem. It seems that the question could be rephrased as: "What is the correct ASP.NET Core way to design routes and resolve such situations with multiple parameters?"
TLDR: use Attribute-Based Routing
This is the new type of routing which was added in ASP.MVC 5. Since then the old routing mechanism has been regarded as "Conventional-Based". ASP.NET Core currently allows mixing of both. I will provide a detailed example of how to solve the described problem using the Attribute-Based Routing only because the new approach provides the following advantages:
Route information is moved closer to controller actions, hence code is easier to understand and troubleshoot
Controller names and their method are decoupled from route names
Moving on to the solution. Let's assume that we have 2 models: Application and Material. I would create 2 distinct controllers to manage each. From your example, I understand that the relationship between these domain entities is one-to-many e.g. one Application could have many Materials. This suggests the following routes:
GET: applications/{applicationId}
GET: applications/{applicationId}/materials/{materialId}
...with a look back at principles of Restful Routing.
The ApplicationController would then look like this:
[Route("applications")]
public class ApplicationController
{
[HttpGet("{applicationId}")]
public IActionResult Index(string applicationId)
{
// return your view
}
}
While the MaterialController would look like this:
[Route("applications/{applicationId}/materials")]
public class MaterialController
{
[HttpGet("{materialId}")]
public IActionResult Index(string applicationId, string materialId)
{
// return your view
}
}
The call to UseMvc(..) extension method in Configure method of Startup.cs can now be simplified to just:
app.UseMvc();
Hope you'll find it useful!

Work out if Controller is in an Area

Is there a way to check whether the Controller which invoked a method comes from a Controller which is within an Area?
For example, I have a class which inherits from AuthorizeAttribute e.g.
public class CustomAuthorize: System.Web.Mvc.AuthorizeAttribute
{
public CustomAuthorize()
{
...
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// TODO - Check if the controller is from an Area
}
}
I then have some controller actions which are decorated with relevant Roles (as well as a few other custom attributes) e.g.
[CustomAuthorize(Roles ="Administrator")]
[HttpGet]
public virtual ActionResult Index()
{
...
}
In the TODO section above, I'd like to see if the Controller is one of the controllers from one of my Areas. I know that my controllers which are in an area will be in the ProjectName.Areas.xxx.Controllers namespace (where xxx is the Area name), whereas ones which aren't will be in the ProjectName.Controllers namespace.
Is there some way (using reflection perhaps?) that from within the AuthorizeCore function above that I can work out the specific area (or namespace that it came from) so that I can implement some custom functionality?
You can get it from the RouteData.DataTokens:
httpContext.Request.RequestContext.RouteData.DataTokens["area"]
That will return null if your controller is not in an area or the name of the area if your controller is in an area.
I'm not too familiar with MCV's Area concept, but I've found this link from a Google search. Perhaps it may help you out.
ASP.NET MVC - Get Current Area Name in View or Controller

Remove controller name from URL with Attribute routing

I want to remove controller name from URL for specific Controller.
My Controller name is Product
I found some link to do this
Routing with and without controller name
MVC Routing without controller
But all the above links done in route config file. and those are affecting other controller too. I want to do it using Attribute Routing.
Can it is possible? As I want to do this for only Product controller.
I have tried to do it on action like this
[Route("Sample/{Name}")]
but it is not working.
Gabriel's answer is right, however, it can be a bit misleading since you're asking for MVC and that answer is for Web API.
In any case, what you want is to put the annotation over the class definition instead of an action method. MVC example would be like:
[RoutePrefix("SomethingOtherThanProduct")]
public class ProductController : Controller
{
public ActionResult Index()
{
...
return View();
}
}
I'm also dropping this as an answer since you may find the following article helpful: [Attribute] Routing in ASP.NET MVC 5 / WebAPI 2
Make sure you set the RoutePrefix attribute on the whole controller class, as well as using the Route attribute on the action.
[RoutePrefix("notproducts")]
public class ProductsController : ApiController
{
[Route("")]
public IEnumerable<Product> Get() { ... }
}

How to add a .Net MVC request filter to prevent action calls based on role membership?

In .Net MVC5, how would one add a request filter to prevent action calls based on role membership?
See this comment:
wouldn't it make more sense to use a request filter to prevent the
action call on the controller in the event that the current user did
not have the right role membership instead of trying to mix the auth
logic in to the business logic?
Thank you.
My best solution for this is using:
[AuthorizeAttribute]
You can place it as a normal attribute is used in c# mvc, like for ex:
[Authorize]
public ActionResult AuthenticatedUsers()
{
return View();
}
You can also use it in top of the controller like this:
[Authorize]
public class HomeController : Controller
{
}
And if you want it do be depedent on roles, you just simple give one parameter to this attribute like this:
[Authorize(Roles = "Admin, Super User")]
public ActionResult AdministratorsOnly()
{
return View();
}
[Authorize(Users = "Betty, Johnny")]
public ActionResult SpecificUserOnly()
{
return View();
}
Here is some more detailed information for your question which I'd suggest would help you alot.
Good luck!

Categories