运行mvc Web应用程序时出错?

时间:2016-05-17 11:03:57

标签: c# asp.net-mvc

我是MVC技术的新手。我运行MVC应用程序时遇到错误。我附上了我的代码和错误图片。请解决我的问题。

enter image description here

我添加了一个名为Default1Controller.cs的控制器,该控制器的代码是

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

namespace MvcApplication1.Controllers
{
    public class Default1Controller : Controller
    {
        public string Index()
        {
            return "hello";
        }
        public ActionResult About()
        {
            return View();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

Just change this in your RouteConfig:

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

To this:

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

Because by default it runs HomeController.

But if you don't want to change the RouteConfig you could rename your controller to HomeController or type this in your address bar:

http://localhost:55416/default1

答案 1 :(得分:1)

There are a couple of things to correct here.

By convention, the initial default controller is called HomeController. ASP .NET MVC uses the name of the controller "Home" to build the routes. Take a look at a default route config:

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

Note that the defaults line maps some options to the main route. Specifically, it provides "default" values for the controller name and action name, and then specifies that the id component is optional.

In this setup, when no controller or action are specified, Home/Index would be the default. So this:

http://localhost/

Would default to this route:

http://localhost/Home/Index

But your controller is named Default1Controller. So you'd have to do one of the following:

  1. Rename your controller to HomeController (or whatever is in your route config); or
  2. Update your route config to default to Default1 instead of Home; or
  3. Specify the route in your URL: http://localhost/Default1/Index

Additionally, your Index action doesn't look quite right:

public string Index()
{
    return "hello";
}

I'm not sure the framework is going to know what to do with that string. Actions need to return ActionResult (or any derived class thereof). Something like this:

public ActionResult Index()
{
    return Content("hello");
}

There may be some functionality in the framework to automatically wrap a value in a ContentResult, I'm not sure. But at the very least, it semantically seems to make more sense to return an ActionResult consistently. This would also help differentiate between MVC controllers and API controllers.

Besides, even if that Index action does work, or even when using a ContentResult, all it's returning is a string and not an actual page. Is that what you want for your default action when a user visits the site?