user1688401
user1688401

Reputation: 1871

Access asp.net mvc controller ActionResult

i want to access action result in controller(my controlelr is HotelController action is Index)

(http://localhost:9001/Hotel/Index) it gives below error

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Hotel/Index

Hotel controller

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)
        {

.......

and this is my 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);
            }
        }
    }
}

Upvotes: 0

Views: 1129

Answers (2)

von v.
von v.

Reputation: 17108

Based on the route config you posted, your URL should be with culture, for example:

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

Notice the en before Hotel. It could be any value that is valid in your database.

Upvotes: 0

Muhammad Omair
Muhammad Omair

Reputation: 797

Because this is due to Razor engine unable to find Thanks action in Hotel controller. You need to make a Thanks action with in Hotel controller like this:

public class HotelController : Base.BoxyController
{

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

}

And also make sure to create a view in Hotel folder with your html code.

Upvotes: 3

Related Questions