Harshana Narangoda
Harshana Narangoda

Reputation: 783

An error occurred while processing your request. MVC 4

I'm creating a MVC 4 application, I had error like following.

enter image description here

I tried lot of things but I can't find what is the problem.here is my Controller source

    public ActionResult Index(string EventId)
    {

        HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["User"];

        if (cookie != null)
        {
            string Type = (cookie["Type"] == null || cookie["Type"] == "") ? null : cookie["Type"].ToString();
            string Username = (cookie["Username"] == null || cookie["Username"] == "") ? null : cookie["Username"].ToString();
            ViewBag.Message = Type;
            ViewBag.Username = Username;


            try
            {
                string ReplaceEventID = EventId.Replace('-', '/');

                ViewBag.Message = ReplaceEventID;
                IEnumerable<Job> JobListRelatedToEvent = DBContext.Jobs.Where(x => x.EventId == ReplaceEventID);
                return View(JobListRelatedToEvent);
            }
            catch
            {
                return View();
            }
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

UPDATE: When it run on my local machine it works fine, but after i published to the server I got this error.

Can anyone tell about what's the wrong?

Upvotes: 7

Views: 31477

Answers (3)

satya
satya

Reputation: 261

I am facing the same issue and this issue we are getting if the custom error is on.

What you need to do the changes in web.config and add the below code. So, you will get the actual error of the application or in code level.

<system.web>
      <customErrors mode="Off" />

Now insted of generic page of IIS it will shows the error.

Upvotes: 26

Auri Rahimzadeh
Auri Rahimzadeh

Reputation: 2263

I don't know if it will help you, but I ran into an issue where the POST action had [RequireHttps], but the GET didn't. That caused the issue for me. So, check you don't have restrictions on one verb and not the other, especially with a form post.

The issue didn't show until I published to Production because DEBUG directives removed the [RequireHttps] attribute :)

Upvotes: 0

Amit
Amit

Reputation: 823

TRY THIS :

 public ActionResult Job(string EventId)
        {

            HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["User"];

            if (cookie != null)
            {
                string Type = (cookie["Type"] == null || cookie["Type"] == "") ? null : cookie["Type"].ToString();
                string Username = (cookie["Username"] == null || cookie["Username"] == "") ? null : cookie["Username"].ToString();
                ViewBag.Message = Type;
                ViewBag.Username = Username;


                try
                {
                    string ReplaceEventID = EventId.Replace('-', '/');

                    ViewBag.Message = ReplaceEventID;
                    IEnumerable<Job> JobListRelatedToEvent = DBContext.Jobs.Where(x => x.EventId == ReplaceEventID);
                    return View(JobListRelatedToEvent);
                }
                catch
                {
                    return View();
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
  }

Upvotes: -4

Related Questions