0

I cannnot get my ASP .NET Core MVC site to route to different controller other than Home. I changed the default route in Startup (it is the only route):

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

my ProfileController looks like:

public class ProfileController : Controller
{
    [HttpGet("index")]
    public IActionResult Index() 
    {
        return View();
    }
    ...
}

But all I get is 404 returned form the Kestrel server on navigating to base URL.

3
  • try with Profile and Index instead of profile and index Commented Dec 14, 2016 at 12:44
  • no difference.. Commented Dec 14, 2016 at 12:52
  • Yep...I posted an answer with an working example. The route is note case sensitive, so Profile and profile is the same. Commented Dec 14, 2016 at 12:57

1 Answer 1

1

I've just created a new project and it worked for me. Maybe you're doing something else wrong.

Here's what I did.

  • Create a new asp.net core web application project (choose MVC template)
  • Update the default route in the Startup.cs:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
      loggerFactory.AddConsole(Configuration.GetSection("Logging"));
      loggerFactory.AddDebug();
    
      if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
      } else {
        app.UseExceptionHandler("/Home/Error");
      }
    
      app.UseStaticFiles();
    
      app.UseMvc(routes => {
        routes.MapRoute(
            name: "default",
            template: "{controller=profile}/{action=index}/{id?}");
      });
    }
    
  • Create the Profile Controller:

    public class ProfileController : Controller {
      // GET: /<controller>/
      public IActionResult Index() {
        return View();
      }
    }
    
  • Create the Index View for the Profile Controller:

enter image description here

  • Run the project and the Profile's Index page should open.

UPDATE:

The problem was the [HttpGet("index")] in the Profile Controller.

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

3 Comments

bugger it was the HttpGet attribute ?!
Yep. If I insert [HttpGet("index")] on my method It stop working. I guess HttpGet is for Web API not MVC. If you're building a web api and needs a web page, you can mix API and MVC.
No I can use HttpGet and HttpPost attribute elsewhere - ineed I prefer them, just the default index gets screwed it seems if you use it.

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.