Explicit .NET MVC Views

Hey, another newbie here, just playing with .NET MVC. My main task is to have several semi-static pages on URLs, for example:

  • /about/
  • / o / contacts /
  • / o / work /

I use the controller for Static and connect the following route:

routes.MapRoute(
  "About",
  "about/{id}",
  new { controller = "Static", action = "Index", id = UrlParameter.Optional }
);

Everything seems to be fine, since I have a Static controller with an Index method that uses the switch statement to determine which page to view. I am using the RedirectToAction () function to call other Static controller actions to display pages with other views. My views:

  • /Static/About.aspx
  • /Static/Contacts.aspx
  • /Static/Jobs.aspx

, , // /Static/Contacts, , URL.

, : ? ?

, ~ K.

+3
3

. , switch Index, (, About, Contacts, Job) .

Static :

public ActionResult Index()
{
    return View();
}

public ActionResult About()
{
    return View();
}

public ActionResult Contacts()
{
    return View();
}

public ActionResult Jobs()
{
    return View();
}

- , Contacts Jobs, .

:

return View("ViewName");

View(). :

return View("ViewName", Model);
+3

Static About. .

public ActionResult About() 
{
   return View ("About");
}

//Jobs() and Contacts() follow the same pattern

3 :

routes.MapRoute(
  "Jobs",
  "about/jobs",
  new { controller = "About", action = "Jobs" }
);

routes.MapRoute(
  "Contact",
  "about/contact",
  new { controller = "About", action = "Contact"  }
);

routes.MapRoute(
  "About",
  "about/",
  new { controller = "About", action = "About" }
);
+2

URL.

- .

.

Admin. .

index.aspx

<ul>
    <li>
        <%= Html.ActionLink("Evaluaties", "Evaluaties", "Admin")%></li>
    <li>
        <%= Html.ActionLink("ONAS aanbieders", "Index", "ONASAanbieder")%></li>
    <li>
        <%= Html.ActionLink("ONAS aanbod", "Index", "ONASAanbod")%></li>
    <li>
        <%= Html.ActionLink("Helpbox", "Index", "HelpBox")%></li>
</ul>

    public ActionResult Index() {
        return View();
    }

    public ActionResult Evaluaties() {
        return View();
    }

it works as you described, no need to change routes. obviously i have Evaluaties.aspxin my folder Adminin the folder Views.

gives me this url: http://localhost:50152/Admin/Evaluaties

+1
source

Source: https://habr.com/ru/post/1765627/


All Articles