Question

The page in question is a setup page for my software. I want the user to paste a license key into a textarea . They then have the option to validate it (validation done on the controller) and they can 'apply' it ( saved in a reg key on the server'

However I seem to have hit a maximum length that can be sent to the controller. The Key is being passed as a string but it fails with an error is its longer than 2142 characters . ( My Keys are about 2500 ish)

So I thought i'd be clever and use slice to split the very long license Key into 2 or 3 parts, but then I seem to hit a slightly shorter 'overall' length restriction.

So here is the code slitting into 2 strings and it works fine if I leave the overall length at 1800 However if I try adding a 3rd or increase the overall length to over 2000 (roughly) I get an error and a break point at the controller is never reached.

Controller

  Function UpdateLicense(Params As ConfigParams, NewLicenseKeyA As String, NewLicenseKeyB As 
   String) As EmptyResult
   Dim LicKey As String = NewLicenseKeyA + NewLicenseKeyB
   'just testing
   Return Nothing
  End Function

And here is the View

$(document).ready(function () { 
$("#CheckLicense").bind("click", function () {
$.ajax({
   url: '@Url.Action("UpdateLicense", "Home")',
   data: { NewLicenseKeyA: ($("#NewLicKey").val()).slice(0, 900), NewLicenseKeyB: $("#NewLicKey").val()).slice(900, 1800) },
     success: function (data) {
       alert("Success!");  
     },
      error: function (xhr, ajaxOptions, thrownError) {
         //some errror, some show err msg to user and log the error  
      alert(xhr.responseText);
     }
});

I am guessing there is an overall maximum length for the URL which is stopping me and by splitting the string I am adding more to the URL and hence shortening the space I have left for sending the licence Code.

Any ideas.. remember I am a novice to MVC and Web. If my assumptions are correct, then I was thinking that maybe I could make multiple calls to the controller each with 1000 chars and then call the final one which would join them all together. Can that be possible?

OK Update: I have a work around that works for now. Here is the updated controller

Function UpdateLicense(Params As ConfigParams, NewLicenseKeyPart As String, KeyName As String) As EmptyResult 
    Dim NewLicenseKey As String
    Select Case KeyName
      Case "A"
        RegistryHelpers.SetRegistryValue("Software\FormeWare\SCAR\", "LicKeyA", NewLicenseKeyPart)
        Return Nothing
      Case "B"
        Dim LicKeyPartA = RegistryHelpers.GetRegistryValue("Software\FormeWare\SCAR\", "LicKeyA", False)
        NewLicenseKey = LicKeyPartA + NewLicenseKeyPart
        'Proceed to Process
      Case Else
        'hmmmmm
    End Select
    Return Nothing
    End Function

So this works , but seems to be a pretty rude way of achieving what I want.. What would be the 'right' way of doing this?

Était-ce utile?

La solution

Here's my model:

namespace MvcApplication3.Models
{
    public class LicenseModel
    {
        public LicenseModel()
        {
            ID = 1;
            LicenseKey = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." +
                "In auctor nisi sed ultricies consectetur. Suspendisse euismod " +
                "sollicitudin tortor, nec accumsan eros facilisis sit amet. Integer" +
                "non felis vel risus fermentum accumsan. Vivamus gravida orci in libero" +
                "semper, nec ultrices turpis sodales. Quisque sit amet cursus dui, ac " +
                "pharetra eros. Morbi ultricies risus ut turpis molestie imperdiet. ";
        }

        public bool Save()
        {
            //do whatever to do to save
            return true;

        }

        public int ID { get; set; }
        public string LicenseKey { get; set; }
    }
}

My controller:

namespace MvcApplication3.Controllers
{
    public class LicenseController : Controller
    {
        //
        // View License/

        public ActionResult ViewLicense()
        {
            LicenseModel model = new LicenseModel();
            return View(model);
        }


        [HttpPost]
        public ActionResult UpdateLicense(int id, LicenseModel model)
        {
            if (ModelState.IsValid)
            {
                model.Save();

            }

            return View("ViewLicense", model);
        }

    }
}

And my strongly typed view, with model declaration at the top:

@model MvcApplication3.Models.LicenseModel
<h2>ViewLicense</h2>


@using (Html.BeginForm("UpdateLicense", "License", FormMethod.Post))
{<div>
    <div>@Html.HiddenFor(m=>m.ID) License Key :</div>
    <div>@Html.EditorFor(m=>m.LicenseKey)</div>
</div>
<div>
    <button type="submit" id="btnsubmit" value="SUBMIT">SUBMIT</button>
</div>
}

Notice that I did not use ajax for posting.

You will need to modify your RouteConfig.cs:

namespace MvcApplication3
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "License", action = "ViewLicense", id = UrlParameter.Optional }
            );
        }
    }
}

Put a breakpoint in the controller and see your changed license key is in the model in the UpdateLicense method.

Hope it helps!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top