0

In my current project I needed to make a new .NET MVC controller that is passed a parameter. I want this new controller to use the same javascript files as the original home controller.

RouteConfig.cs

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
           name: "Controller2",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Controller2", action = "Index", id = UrlParameter.Optional }
       );

Here is the Controller2 code:

public class Controller2 : Controller
{

    public ActionResult Index(int id)
    {
       //do stuff here
        return View("~/Views/Home/Index.cshtml");
    }
}

When I access Controller2 via this link: http://localhost/AppName/Controller2/index/1

The application navigates to the correct index file but looks for the css files in a location where they are not, namely: http://localhost/AppName/Controller2/index/dist/css/app.css

The files the index for Controller2 are really in:

http://localhost/AppName/dist/css/app.css

It must have something to do with my Routing but I can't figure it out.

How do I fix this problem?

Thanks, Matt

1 Answer 1

1

Use absolute, or even better, application relative links for your styles:

<link href="~/dist/css/app.css">

Note the "~". If the URL that ends up in the browser does not start with a / or a scheme, it will be relative to the current URL of the page.

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

1 Comment

Thank you! This fixed the issue I was having!

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.