C# mvc4 - direct URL to controller

时间:2015-06-15 14:35:43

标签: c# asp.net-mvc-4 url redirect

I have an external URL that points directly to a function in my controller but when I click on it for the first time, instead of reaching the page, I get to the home page. However, on the second click (and the following ones), I can reach the desired page.

Is there something I should add to my function or controller to allow to have it reached on the first click? Or would it be my sessions that aren't initialized on the first time? I tried debugging, but I never reached the desired function on the first click.

Here is my external URL pointing to my controller: http://localhost/careers/postings/index?pvnID=HC-1505-000533

and here is the method from the controller:

public ActionResult Index(string pvnID)
    {

        //if pvnid matches legacy PVN application pater -> refirect to legacy application
        Regex pattern = new Regex("[A-Z]{3}-\\d{3,4}");
        Match match = pattern.Match(pvnID);
        if (match.Success)
        {
         //redirect to legacy application
        }
        else 
        {
            model.Job = jobPostingMgr.GetJobPosting(pvnID);

            string maxSalary = "";
            string minSalary = "";

            if(model.Job.MaximumSalary.ToString() != "0.00"){
                maxSalary = String.Format("{0:C}", model.Job.MaximumSalary);
            }
            if (model.Job.MinimumSalary.ToString() != "0.00")
            {
                minSalary = String.Format("{0:C}", model.Job.MinimumSalary);
            }

            if (maxSalary != "" && minSalary != "")
            {
                ViewBag.SalaryRange = minSalary + " - " + maxSalary;
            }
            else if (maxSalary == "" && minSalary != "")
            {
                ViewBag.SalaryRange = minSalary;
            }
            else if (maxSalary != "" && minSalary == "")
            {
                ViewBag.SalaryRange = maxSalary;
            }


            //data processing
            Session["category"] = model.Job.Category;
            model.Job.Category = miscInfoMgr.GetAJobCategory(model.Job.Category).Description;
            model.Job.CollegeCode = PvnHelper.GetCollege(model.Job.CollegeCode).Description;
            Session["pvnid"] = pvnID;


            //Change to lower case 
            model.Job.DisplayJobTitle = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(model.Job.DisplayJobTitle.ToLower());
            model.Job.Department = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(model.Job.Department.ToLower());
            model.Job.CollegeCode = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(model.Job.CollegeCode.ToLower());
        }
        return View(model);
    }

Thanks a lot in advance for your help.

1 个答案:

答案 0 :(得分:1)

You might have forgotten to specify name of the controller in Html.ActionLink() parameter. Try Html.ActionLink("actionname","controllername");