asp.net mvc - sending long strings to the controller -
the page in question setup page software. want user paste license key textarea . have option validate (validation done on controller) , can 'apply' ( saved in reg key on server'
however seem have hit maximum length can sent controller. key being passed string fails error longer 2142 characters . ( keys 2500 ish)
so thought i'd clever , use slice split long license key 2 or 3 parts, seem hit shorter 'overall' length restriction.
so here code slitting 2 strings , works fine if leave overall length @ 1800 if try adding 3rd or increase overall length on 2000 (roughly) error , break point @ controller never reached.
controller
function updatelicense(params configparams, newlicensekeya string, newlicensekeyb string) emptyresult dim lickey string = newlicensekeya + newlicensekeyb 'just testing return nothing end function
and here 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, show err msg user , log error alert(xhr.responsetext); } });
i guessing there overall maximum length url stopping me , splitting string adding more url , hence shortening space have left sending licence code.
any ideas.. remember novice mvc , web. if assumptions correct, thinking maybe make multiple calls controller each 1000 chars , call final 1 join them together. can possible?
ok update: have work around works now. here updated controller
function updatelicense(params configparams, newlicensekeypart string, keyname string) emptyresult dim newlicensekey 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 process case else 'hmmmmm end select return nothing end function
so works , seems pretty rude way of achieving want.. 'right' way of doing this?
here's 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 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 typed view, model declaration @ 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 did not use ajax posting.
you need modify 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 breakpoint in controller , see changed license key in model in updatelicense method.
hope helps!
Comments
Post a Comment