Search code examples
c#asp.net-corerazor

Layout page is not displayed, working in other Views except index. C# ASP.NET CORE


Hi fellow stackoverflowers.

Long story short, I am currently building an User interface in ASP.net Core MVC. I have created an Action controller that returns an view. Which looks as following.

public class VehicleHomeController : Controller
{
    private readonly IVehicleHomeService _vehicleHomeService;

    private readonly IMapper _mapper;

    public VehicleHomeController(IVehicleHomeService vehicleHomeService, IMapper mapper)
    {
        this._mapper = mapper;
        this._vehicleHomeService = vehicleHomeService;
    }
    public IActionResult Index()
    {
        return View();
    }

    public async  Task<IActionResult> Edit(int id)
    {
        var vehicleHome = await _vehicleHomeService.GetVehicleHomeById(id);

        var vehicleHomeResource = _mapper.Map<VehicleHome, VehicleHomeViewModel>(vehicleHome);

        return View(vehicleHomeResource);
    }
}

Returning the Index view works like a flow. It uses the layoutpage properly. However when creating the Edit View the Layout is not displayed properly. Both action controllers looks the same in the code that provides the layout.

@{
     Layout = "_Layout";
}

I've searched for an answer. One common solution was defining layout = "/Views/Shared/_Layout.cshtml";. However this does not work for my application. Does anyone had any similar problems or any tips?


Solution

  • Problem solved.

    The path for finding the layout CSS and Logos then went wrong. The problem is that the code tries to get custom CSS and logos inside PathForTheApplicationLocalHost/Edit/images/logo.svg. And PathForTheApplicationLocalHost/Edit/css/custom.css.

    While the correct path should be PathForTheApplicationLocalHost/css/custom.css. I solved this by defining the following inside _Layout.cshtml

    <link rel="stylesheet" type="text/css" href="~/css/custom.css">
    

    This made the path correct (PathForTheApplicationLocalHost/css/custom.css) from calling layout in the Edit view.