How to call default action and send parameter without action name - .net-core

public class bController : Controller
{
public IActionResult Index(string id)
{
return View();
}
}
I have controller like this. When I want to call index action I need the go to url http://localhost/b/index/id .
But ı want to like this http://localhost/b/id how can i set this action default and call like this. Give me idea pls
Thanks.

If anyone wonder how to do this this is the answer.
public class bController : Controller
{
[Route("b/{id}")]
public IActionResult Index(string id)
{
return View();
}
}

Related

ASP.NET Core Route inside controller

I am trying to route url with and without parameter to two different methods but for some reason it always start first one.
Here is controller code:
public class ProductController : Controller
{
[Route("")]
[Route("Product")] //If i delete this one then it works how it is intended
public IActionResult Index()
{
//It always start this one
....
}
[Route("Product/{GroupID?}")]
public IActionResult Index(int GroupID)
{
....
}
}

Pass a string to view for use as parameter in Html.Partial

I have a Controller and a Model:
public class HomeController : Controller
{
public ActionResult Index()
{
var ribbon = new RibbonModel();
ribbon.Link = "_RibbonIndex";
return View(ribbon);
}
}
public class RibbonModel
{
public string Link { get; set; }
}
I want to use it in a view to call a partial view:
#model MyNamespace.Controllers.RibbonModel
#Html.Partial(model => model.Link)
I get an error (bad translated):
'lambda-expression' cannot converted to 'string'...
I tried it also with ViewBag and ViewData, nothing works. Any idea? Is there a better way to achieve this?
Thank you!

How do you set the default route in MVC6 when using attributes?

In MVC5 you could set the default route using the following attribute on a controller?
[Route("{action=index}")]
What is the equivalent of this in MVC6?
Update
This is the code I had in MVC5
[Route("{action=index}")]
public class StaticPagesController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
public ActionResult Index()
{
return View();
}
}
I have not been able to work out how to do the equivalent in MVC6 but I've been able to get the same functionality working using the following:
[Route("[action]")]
public class StaticPagesController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
[Route("/[action]")]
[Route("/")]
public ActionResult Index()
{
return View();
}
}
You can decorate your specific action method which you want to be the default action with Route attribute and pass [controller] as the route template to that. So anytime you get a request for yoursite/yourcontroller, the incoming request will be redirected to this specific action method.
public class SettingsController : Controller
{
public IActionResult Index()
{
return View();
}
[Route("[controller]")]
public IActionResult OtherIndex()
{
return Content("This will be the response for mySite/Settings request");
}
}
Edit : As per the comment
I don't want to include the controller name in the URL.I want it to be
domain.com/About rather than domain.com/StaticPages/About
Using attribute routing, you can decorate your action method with the Route attribute and give [action] as the template name.
public class StaticPagesController : Controller
{
[Route("[action]")]
public IActionResult About()
{
// This action method will be executed for "yourSite/About" request
return View();
}
}
With the above approach, you cannot have 2 action method's with the same name in your app ( Ex : You cannot have an About action method in HomeController and StaticPagesController)

How to get roles array from Authorize Attribute of certain action in ASP.NET MVC?

Suppose I have following Controller and action with authorization Attribute:
public class IndexController : Controller
{
//
// GET: /Index/
[Authorize(Roles="Registered")]
public ActionResult Index()
{
return View();
}
}
I've searched over the entire Internet and not found an answer for this simple question: how to get the roles annotated to an especific Action/Controller? In this case: Index Action has: string[] = {"Registered"}
Finally I found the solution! Was more easy than I thought! ahahha I need extend a class from AuthorizeAttribute and use it in actions. The information I need is the attribute "Roles" of the inherited class:
public class CustomAuthorizationAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var roles = this.Roles;
base.OnAuthorization(filterContext);
}
}
And on Index Controller:
public class IndexController : Controller
{
//
// GET: /Index/
[CustomAuthorizationAttribute(Roles = "Registered")]
public ActionResult Index()
{
return View();
}
}

How to append a prefix to action name according to a particular route

I'm using asp.net mvc 4 and web api. My route is like this:
/api/{controller}/jqGrid/{action}/{id}
for example, if the route is :
/api/User/jqGrid/List
I hope it will route to the action name "jqGrid_List" of the User controller.
How can I achieve this?
hmm, I don't know if it's acceptable to answer my own question. I found out a solution.
First of all, I need to add a JqGridControllerConfiguration attribute to replace the default action selector applied to the controller with my one.
[JqGridControllerConfiguration]
public class UserController : ApiController
{
// GET: /api/User/jqGrid/List
[HttpGet]
public JqGridModel<User> jqGrid_List()
{
JqGridModel<User> result = new JqGridModel<User>();
result.rows = Get();
return result;
}
}
Here's the code of JqGridControllerConfiguration:
public class JqGridControllerConfiguration : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Services.Replace(typeof(IHttpActionSelector), new JqGridActionSelector());
}
}
in JqGridActionSelector, the "action" is modified if a "jqGrid/" exists in the request URL.
public class JqGridActionSelector : ApiControllerActionSelector
{
public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
{
Uri url = controllerContext.Request.RequestUri;
if (url.Segments.Any(s => string.Compare(s, "jqGrid/", true) == 0))
{
controllerContext.RouteData.Values["action"] = "jqGrid_" + controllerContext.RouteData.Values["action"].ToString();
}
return base.SelectAction(controllerContext);
}
}
Not sure why you'd want to do this. But you can still create a "jqGrid_List" action in your User controller and set an ActionName for it, and it'll work.
UserController:
[HttpGet, ActionName("List")]
public string jqGrid_List()
{
return "WORKS";
}
Your Route:
routeTemplate: "api/{controller}/jqGrid/{action}/{id}"

Resources