访问asp.net mvc控制器ActionResult

时间:2013-04-10 12:14:33

标签: asp.net-mvc asp.net-mvc-3 compiler-errors asp.net-mvc-routing

我想在控制器中访问动作结果(我的控制器是HotelController动作是索引)

http://localhost:9001/Hotel/Index)它给出了以下错误

无法找到资源。

描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。

请求的网址:/ Hotel / Index

酒店管制员

public class HotelController : Base.BoxyController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            ViewBag.Title = "SonDakka - Otel";
        }

        public ActionResult Index(string culture)
        {

.........

BoxyController

 public class BoxyController : MainController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

..........

MainController

  public class MainController : SiteController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

...

SiteController

  [ExitHttpsIfNotRequired]
    public class SiteController : Controller
    {
        public Account Me { get; set; }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

.......

这是我的global.asax

using System;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Tourism.Data;
using Tourism.Data.Mvc.Authorization;
using Tourism.Data.Mvc.Routing;

namespace Tourism
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(TourismContext db, RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            var cultures = db.Cultures.Select(c => c.Code).ToArray();

            routes.MapRoute
                (
                    "Ajax",
                    "{culture}/{controller}/{action}/{id}",
                    new { id = UrlParameter.Optional },
                    new { culture = new ArrayRouteConstraint(true, cultures), controller = new ArrayRouteConstraint(true, "Ajax") }
                ).RouteHandler = new GlobalizedRouteHandler();

            routes.Add
                (
                    "Page",
                    new GlobalizedPageRoute
                        (
                            "{culture}/{path}",
                            null,
                            new RouteValueDictionary { { "culture", new ArrayRouteConstraint(true, cultures) } },
                            new GlobalizedRouteHandler()
                        )
                );

            routes.Add
                (
                    "Route",
                    new GlobalizedRoute
                        (
                            "{culture}/{path}/{slug}/{id}",
                            new RouteValueDictionary { { "culture", UrlParameter.Optional }, { "path", UrlParameter.Optional }, { "slug", UrlParameter.Optional }, { "id", UrlParameter.Optional } }, 
                            new RouteValueDictionary { { "culture", new ArrayRouteConstraint(false, cultures) } },
                            new GlobalizedRouteHandler()
                        )
                );
        }

        protected void Application_Start()
        {
            Database.SetInitializer<TourismContext>(null);

            using (var db = new TourismContext())
            {
                #if !DEBUG

                if (!db.Database.CompatibleWithModel(true))
                {
                    System.Web.HttpRuntime.UnloadAppDomain();

                    throw new Exception("Veritabanı değişikliği tespit edildi.");
                }

                #endif

                AreaRegistration.RegisterAllAreas();

                RegisterGlobalFilters(GlobalFilters.Filters);

                RegisterRoutes(db, RouteTable.Routes);
            }
        }

        protected void Application_PostAuthenticateRequest()
        {
            if (Request.IsAuthenticated)
            {
                Context.User = System.Threading.Thread.CurrentPrincipal =
                    new AuthorizationPrincipal(Context.User.Identity);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

因为这是由于Razor引擎无法在酒店控制器中找到感谢操作。您需要在酒店控制器中执行感谢操作,如下所示:

public class HotelController : Base.BoxyController
{

 public ActionResult Thanks(string culture)
 {
    return View();
 }

}

并确保使用您的html代码在Hotel文件夹中创建一个视图。

答案 1 :(得分:0)

根据您发布的路线配置,您的网址应为culture,例如:

http://localhost:9001/en/Hotel/Index

请注意en之前的 Hotel 。它可以是数据库中有效的任何值。