Joy
Joy

Reputation: 7028

HttpGet returns 404 in Mvc core 2.0

public class SurveyController : Controller
{
    public IActionResult Index()
    {
        var surveys = new List<int>{1};
        return View(Surveys);
    }

    [HttpGet("conditions")]
    public IActionResult GetConditions()
    {
        List<int> Conditions = new List<int{1};
        return View("Conditions",Conditions);
    }
}

Now the views are under

Views/Survey/Index.cshtml

Views/Survey/Conditions.cshtml

The route Survey/conditions is returning 404 .

Does anybody has any idea ?

My startup.cs is -

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

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

Upvotes: 1

Views: 1904

Answers (2)

DavidG
DavidG

Reputation: 118987

The attribute you have on the action:

[HttpGet("conditions")]

is specifying that you want the URL to be http://whatever/conditions. Instead you should use:

[HttpGet("/survey/conditions")]

If you're looking to control your routing with better granularity, you should use the Route attribute instead. For example:

[Route("[controller]")] //Set the prefix for subsequent route attributes
public class SurveyController : Controller
{
    [Route("conditions")]
    public IActionResult GetConditions()
    {
        List<int> Conditions = new List<int{1};
        return View("Conditions",Conditions);
    }
}

Reference Routing to Controller Actions

Upvotes: 4

henoc salinas
henoc salinas

Reputation: 1054

try this:

public class SurveyController : Controller
{
    public IActionResult Index()
    {
        var surveys = new List<int>{1};
        return View(Surveys);
    }

    [HttpGet("conditions")]
    [Route("Survey/conditions")]
    public IActionResult GetConditions()
    {
        List<int> Conditions = new List<int{1};
        return View();
    }
}

or change the name of function in controller:

public class SurveyController : Controller
{
    public IActionResult Index()
    {
        var surveys = new List<int>{1};
        return View(Surveys);
    }

    [HttpGet("conditions")]
    public IActionResult conditions()
    {
        List<int> Conditions = new List<int{1};
        return View();
    }
}

or write the name of function in browser route in browser:

Route = 'Survey/GetConditions'

Upvotes: 1

Related Questions