ASP.NET Core Route Change
Asked Answered
C

1

1

I need to update a handful of static web pages, and I would like to take this time to recreate them using ASP.NET Core (ASP.NET 5 with MVC 6) in Visual Studio 2015. I want to rebuild it using Microsoft's latest technology to make changes easier in the future.

When I start the project on localhost, the default website loads up fine but any of the linked pages break because they route to the /Home controller by default. Also, none of the project's jquery, css or images are found when MVC nests these pages.

In the file Startup.cs, there is the following method:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseIdentity();

    // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

This is how it comes configured out of the box.

Our company does not want /Home (or anything else) stuck on the URL of all of their web pages.

I have created various IActionResult methods for the different pages, but all of them do the same thing:

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

We also have companies that link to our website. Changing the structure of our pages is going to cause other companies to stop and make changes, too.

How would I take a string, such as {controller}/{action}/{id?}, and have that return a typical link, such as action.aspx?id=x?

Alternately, is there a way to tell MVC not to use a template for certain pages?

I apologize if this is stupid basic. I generally work on Windows Forms.

Chandrachandragupta answered 20/6, 2016 at 17:12 Comment(2)
I'm not sure what is advantage of your exercise when you are not willing to take advantage of underlying framework. If MVC is not something you want then I'd suggest to stick with what you have in hand.Tribulation
@Saleem, it is getting hard to do things with Microsoft when it is not in MCV (mainly social media integration). If I never start learning parts of it, I will never know how to use any of it.Chandrachandragupta
G
1

There are two solutions:

Route Template:

app.UseMvc(routes =>
{
    routes.MapRoute(
        "HomeRoute", 
        "{action}/{id}", 
        new 
        { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional
        });
});

Attribute Routes

[HttpGet("")]
public IActionResult Index()
{
    return View();
}

You can create a new project using ASP.NET MVC Boilerplate for a full working example using this method.

Gustie answered 20/6, 2016 at 18:28 Comment(2)
How do I handle the other Controller actions, like Contacts, News, and Sales? As edited above, I get AmbiguousActionException: Multiple actions matched.Chandrachandragupta
I see it. I used Daniel's Answer to give each Action a name (i.e. Contact.html, News.html, Sales.html).Chandrachandragupta

© 2022 - 2024 — McMap. All rights reserved.