1

I work with ASP .NET core, and I have issues.

I have 3 pages which can be created in one controller, but for this pages I must have just one controller and in this conroler, I must have a function which will create separate URL for this 3 pages.

For example:

*http://mydomain/page/*Home, Load, Blog;

For this, I need have navigation, but I will have the same HTML file only will data change from the model.

So I will have 3 navigation button which will navigate user for same page but another content which I will receive from the model, and from controller I just need change page URL.

How I can do this?

8
  • 1
    Actually, generally that's not the way to go. You should be creating different controllers for different pages (generally speaking). Each controller named as 'PageNameController' with an index Action inside of it that leads to returning the view. Is there a specific reason why you need them all in one controller? Commented Jun 19, 2019 at 11:17
  • @Jabberwocky for this i not need to create separate controllers and index pages, because i wil copie past code, i have same view at 3 pages Commented Jun 19, 2019 at 11:19
  • "for this pages I must have just one controller" ...why? That seems like a totally arbitrary requirement which goes against the general principles of MVC. " i have same view at 3 pages" ...I don't see how that has any impact on your use of controllers. Commented Jun 19, 2019 at 11:19
  • 1
    @qunz666 you're going against what MVC is standing for. If you have the same view and the same content then return the view with the model you need from the appropriate controller. MVC is all about separation of concerns and you're planning on doing the opposite. Commented Jun 19, 2019 at 11:20
  • 1
    @qunz666 Yes. You have to study the basics of MVC. Naming conventions so .NET knows how to map your controllers to your view. Your model is going to be your content. and the view will inherit it from the controller if you tell it to. Commented Jun 19, 2019 at 11:24

2 Answers 2

2

You don't want to repeat the HTML? Use one view. And then you can have a controller like this snippet:

class PagesController : Controller
{
    [HttpGet("about")]
    public IActionResult About() => View("MyCommonView", yourModel); // get the model from wherever you plan to

    [HttpGet("contact")]
    public IActionResult Contact() => View("MyCommonView", yourModel);

    [HttpGet("whateverelse")]
    public IActionResult WhateverElse() => View("MyCommonView", yourModel);
}

It's possible to just have one action, but I wouldn't do that. I'd instead have separate views per action, and put the markup that's common for the three actions layout file. That will give the different actions more flexibility.

Sign up to request clarification or add additional context in comments.

Comments

1

One of the way is to decorate your action with Route attribute:

[Route("page")]
public class PagesController : Controller
{
    // you may also use [HttpGet("{pageName}", Name = "PagePath")] instead,
    // to explicitly match HTTP GET requests
    [Route("{pageName}", Name = "PagePath")]
    public IActionResult GetPage(string pageName)
    {
        switch(pageName?.ToLower())
        {
            case "home":
                return View("Page", homeModel);
            case "home":
                return View("Load", loadModel);
            case "home":
                return View("Blog", blogModel);
            default:
                return NotFound();
        }
    }
}

Now you can create your view at Views/Pages/Page.cshtml and generate links with helpers:

@Url.RouteUrl("PagePath", new { pageName = "Home" }) <!-- will produce "/page/Home" string -->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.