Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a nameof() operator for MVC controllers in C#?

Tags:

c#

asp.net-mvc

The newly introduced nameof operator is useful in making my code my "typed".

Instead of

return RedirectToAction("Edit");

we can write

return RedirectToAction(nameof(Edit));

But for to get a controller's name is not that straightforward, because we have a Controller suffix. Just want to know if I want to have a

return RedirectToAction(nameof(Index), controllernameof(Home));

to take the place of

return RedirectToAction("Index", "Home");

how can we implement the controllernameof operator?

like image 872
Blaise Avatar asked Apr 14 '16 14:04

Blaise


People also ask

What is NameOf in C?

A nameof expression produces the name of a variable, type, or member as the string constant: C# Copy.

What is the function for controller in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.


3 Answers

Maybe an extension method like the following would suit your needs:

public static class ControllerExtensions
{
  public static string ControllerName(this Type controllerType)
  {
     Type baseType = typeof(Controller);
     if (baseType.IsAssignableFrom(controllerType))
     {
        int lastControllerIndex = controllerType.Name.LastIndexOf("Controller");
        if (lastControllerIndex > 0)
        {
           return controllerType.Name.Substring(0, lastControllerIndex);
        }
     }

     return controllerType.Name;
  }
}

Which you could invoke like so:

return RedirectToAction(nameof(Index), typeof(HomeController).ControllerName());
like image 118
stephen.vakil Avatar answered Sep 19 '22 05:09

stephen.vakil


No, there is no such possibility. You might be intertested to use T4MVC instead.

T4MVC- a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings in many places.

e.g. instead of

@Html.ActionLink("Dinner Details", "Details", "Dinners", new { id = Model.DinnerID }, null)

T4MVC lets you write

@Html.ActionLink("Dinner Details", MVC.Dinners.Details(Model.DinnerID))
like image 6
TomTom Avatar answered Sep 21 '22 05:09

TomTom


Totally understand your desire to not use magic strings! Between the comments above and this article. I've started using the following in a base controller which my other controllers inherit from:

public RedirectToRouteResult RedirectToAction<TController>(Expression<Func<TController, string>> expression, object routeValues)
{
    if (!(expression.Body is ConstantExpression constant))
    {
        throw new ArgumentException("Expression must be a constant expression.");
    }

    string controllerName = typeof(TController).Name;

    controllerName = controllerName.Substring(0, controllerName.LastIndexOf("Controller"));

    return RedirectToAction(constant.Value.ToString(), controllerName, routeValues);
}

public RedirectToRouteResult RedirectToAction<TController>(Expression<Func<TController, string>> expression)
{
    return RedirectToAction(expression, null);
}

I then use :

 return RedirectToAction<HomeController>(a => nameof(a.Index));

and

 return RedirectToAction<HomeController>(a => nameof(a.Index), new { text= "searchtext" });
like image 6
Jon Ryan Avatar answered Sep 19 '22 05:09

Jon Ryan