Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller not working in mvc 4

I have a controller named as UserController and in that only Index action is being called another action added like as 'home action' is not being called and either calling actionresult/string or returning view

I am getting this error

while running at Google Chrome

{"Message":"No HTTP resource was found that matches the request URI /user/home'.","MessageDetail":"No type was found that matches the controller named 'home'."}

And running at Mozilla Firefox

Whoops! The page could not be found. Try giving it another chance below.

Here is my whole Controller Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Mvc4Application1.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddPlace()
        {
            return View();
        }

        public ActionResult UpdatePlace()
        {
            return View();
        }

        public ActionResult DeletePlace()
        {
            return View();
        } 
    }
}

Here is RouteConfig.cs file

 routes.MapRoute(
     name: "Default",
      url: "{controller}/{action}/{id}",
 defaults: new 
{ controller = "Home", action = "Index",
  id=UrlParameter.Optional }
like image 600
Sonika Koul Avatar asked May 31 '13 11:05

Sonika Koul


2 Answers

A method in a Controller that returns a string will not be routable. It must return an ActionResult (or one of it's derived classes).

Change it to:

public ActionResult Home()
{
    return View("Hello from home");
}

Then in your Views/Home folder, add a view called Home that has something like:

@model string

<p>@Model</p>
like image 170
mattytommo Avatar answered Oct 23 '22 16:10

mattytommo


Here are the following things you need to cross check

  • Inheriting controller from Controller and not from ApiController
  • Make sure Routeconfig has proper Maproute

    Else you need to rename the controller [sounds weird but it might help]

and use ActionResult as return datatype

like image 30
Swapneel Kondgule Avatar answered Oct 23 '22 15:10

Swapneel Kondgule