0

Code background:

I have a model class in Asp.Net Core like this:

namespace urlShortner.Models
{
    public class urlData
    {
        public string longUrl {get; set;}
        public string shortUrl {get; set;}
        public int hitTimes {get; set;} = 0;
        public string creationTimeStamp {get; set;}
    }
}

Now, if use Auto-Implemented property in ViewModel class to store POST request fields directly on it, it's ok, as below:

Razor Page:

<input asp-for="urlDataObj.longUrl"> 
.
.
.
<p style="color: white;">
    @ViewData["lo"]
</p>

ViewModel:

[BindProperty]
public urlShortner.Models.urlData urlDataObj {get; set;}

public void OnPost()
{
    ViewData["lo"] = urlDataObj.longUrl;
}

As shown above, I have a Auto-Implemented property that decorated by [BindProperty] attribute and Razor Page POST data to one of it's field directly, later than it'll be visible on page using ViewData.
I'm trying to avoid this, as it's a security concern and user can easily manipulate the form on the page to POST data for another field/s.
I changed the codes as below:

Razor Page(Changed):

<input asp-for="longUrl"> 
.
.
.
<p style="color: white;">
    @ViewData["lo"]
</p>

ViewModel(Changed):

[BindProperty]
public string longUrl {get; set;}

public urlShortner.Models.urlData urlDataObj {get; set;}

public void OnPost()
{
    ViewData["lo"] = longUrl; // POST data is OK!
    urlDataObj.longUrl = longUrl; // Null reference exception will occurs!
}

Now if I use an Object of type urlShortner.Models.urlData instead of Auto-Implemented property like this:

public urlShortner.Models.urlData urlDataObj = new urlShortner.Models.urlData();

problem will be solved!


Question:

I can simply think this Auto-Implemented property will create baking field sometimes after program compilation, but know nothing when, how etc.
Also, why is it okay to fill the property by POST data, but assign it with a variable is not?
please explain it (life cycle) in clear.

  • 2
    I think, your problem is using `urlDataObj.longUrl = longUrl;`, because you never set this property before and it is null. – Vít Bednář Mar 08 '21 at 05:43
  • 4
    Remove all those unnecessary fluff, and just describe your problem. As it currently stands, its clearly an xy problem, your null reference error has nothing to do with auto implemented property. – Mat J Mar 08 '21 at 05:43
  • @VítBednář This line is the source of problem indeed, but I set ``public string longUrl {get; set;} = "empty";`` and nothing solved! Also bear in mind if create an `Object` instead of a `Property` value is not used too. – Shahaboddin Mar 08 '21 at 05:48
  • @MatJ As you said, stuff removed, but please explain the cause as question is. and if its not related to ``Auto-Implemented property`` then where should I look for the clue since changing the ``property`` helped? – Shahaboddin Mar 08 '21 at 05:53
  • 2
    As far as I understand, your question boils down to: `I have a model, one of its property has its type as custom class, When my request does not include a value for this property, it is always null and when I try to access its value, it is throwing null reference exception`. If that is the case, instead of wondering how auto implemented property works, you should search for what is model binding, how model binder works in asp.net mvc. – Mat J Mar 08 '21 at 06:05
  • 1
    @Shahaboddin Sorry, I am not sure how [BindProperty] works. But for me it still looks like you are trying to set urlDataObj.longUrl to urlDataObj object where `urlDataObj = null`, so it can't have longUrl field. Try add breakpoint to look at values in debugger. I don't know better way how to find where is null. – Vít Bednář Mar 08 '21 at 06:27
  • @VítBednář thanks you a lot for taking effort and explaining! But I can't get you now where you said set ``urlDataObj.longUrl to urlDataObj`` because ``longUrl`` is a property and ``urlDataObj``'s field at the same, could you please clarify? I red these two: [link1](https://www.red-gate.com/simple-talk/dotnet/asp-net/model-binding-asp-net-core/#:~:text=Model%20binders%20work%20under%20the,maybe%20even%20the%20URL%20itself.) [link2](https://www.learnrazorpages.com/razor-pages/model-binding) – Shahaboddin Mar 08 '21 at 06:37
  • 1
    It's not `longUrl`, but `urlDataObj` what is null, because it is never initialized. – René Vogt Mar 08 '21 at 06:43
  • @RenéVogt OK now you mentioned my problem, Yes I knew its the ``urlDataObj`` that caused the problem (As said in post). Would you please explain why it's never initialized? Shouldn't **properties create backing fields** as needed as MSDN said? And why it's not initialized if I wanna assign it myself but OK with ``modelBinding``? – Shahaboddin Mar 09 '21 at 06:42

0 Answers0