Carsten Cors
Carsten Cors

Reputation: 2595

System.NullReferenceException on direct navigation to page

Hi I have a problem when I try to access a page in my application directly. When I navigate within the application to the page, does everything work.

<input type="button"
               class="btn btn-primary"
               id="calulate"
               value="Finanzierungsrate berechnen"
               onclick="location.href ='@Url.Action("calculate", "RatesKit", new { id = "bc338d8d-58b9-4867-9730-6b92a516a496"}) '" />

Here the controller method:

[HttpGet]
public ActionResult calculate(string id, string objectDescription, string objectPrice)
{
    if (string.IsNullOrEmpty(id))
    {
        return RedirectToAction("NotValid");
    }
    else
    {
        if (Validator.ValidateToken(id, out bob))
        {
            OfferRequest request = Session["RequestOffer"] as OfferRequest;
            if (request != null)
            {
                ViewData["Dealer"] = bob;

                //..
            }
        }
        else
        {
            return RedirectToAction("NotValid");
        }

        return View();
    }
}

when I reload the page with F5, the page loaded without Problems. But if I try to access the site directly (Just hit enter in the address bar of Browser), I get the following error message:

System.NullReference

Definitely, theres is no mistake at this point! If I remove the Razor Syntax @DateTime.Now.Year the fault is located at a different point with Razor syntax.

I do not know, why there is an error only with direct access.

Similar problems I've already looked at, unfortunately without solution to my problem.

System.NullReference Exception occurred in App_Web_XXX.dll - Error in Partial View System.NullReferenceException in App_Web_*.dll

Thanks for any advice

Her the full page:

@{
    ViewBag.Title = "Finanzierungsrate berechnen";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<meta charset="utf-8" />
    <div align="center">
    <h2>Finanzierungsrate berechnen</h2>
    <div style="max-width:700px;">
        <div id="ratesView">           
            @{Html.RenderPartial("ViewCalculateRate", ViewData);}
        </div>
    </div>
</div>
<table>
    <tr>
        <td valign="top" class="footerContent" style="padding-top: 20px;color: #878780; font-size: 11px; text-align: left;">
            © @DateTime.Now.Year XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  Alle Rechte vorbehalten. <a href="@Url.Action("Impressum", "RatesKit", new { target = "_blank" })" style="color: #878780; text-decoration:underline; text-decoration-color:#878780">Impressum</a>
        </td>
    </tr>
    <tr valign="top" class="footerContent" style="padding-top: 20px; color: #878780; font-size: 11px; text-align: left;">
        <td>Wir übernehmen keine Gewähr für die Richtigkeit dieser Berechnung.</td>
    </tr>
</table>
<div id="backbutton" style="padding:20px 0; display:inline-block;">
    <input type="button"
           class="btn btn-primary btn-xs"
           id="backButton"
           value="Zurück"
           onclick="location.href ='@Request.UrlReferrer.AbsoluteUri'" />
</div>

Upvotes: 1

Views: 240

Answers (1)

Carsten Cors
Carsten Cors

Reputation: 2595

I got it....

<div id="backbutton" style="padding:20px 0; display:inline-block;">
    <input type="button"
           class="btn btn-primary btn-xs"
           id="backButton"
           value="Zurück"
           onclick="location.href ='@Request.UrlReferrer.AbsoluteUri'" />
</div>

Request does not exist when I go directly to the page. So I was unable to create the button ...

THX Sometimes you have to look at the code times from the outside.

Upvotes: 2

Related Questions