Skip to content

MVC Examples

Stephan van Leeuwen edited this page Apr 23, 2021 · 9 revisions

Usage examples for MVC apps

This page shows how you can use UrlActionGenerator and what methods are generated in what scenario.

Simple controller action

Controller:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Usage:

Url.Actions().Home.Index() // => "/Home/Index"

Simple controller action with parameters (including default parameter values)

Controller:

public class HomeController : Controller
{
    public IActionResult Search(int page, int pageSize = 20)
    {
        return View();
    }
}

Usage:

Url.Actions().Home.Search(1);     // => "/Home/Search?page=1"
Url.Actions().Home.Search(2, 50); // => "/Home/Search?page=2&pageSize=50"

Simple controller action inside an area

Controller:

[Area("Admin")]
public class HomeController : Controller
{
    public IActionResult Index(string str)
    {
        return View();
    }
}

Usage:

Url.AdminActions().Home.Index("value"); // => "/Admin/Home/Index?str=value"
Clone this wiki locally