James123
James123

Reputation: 11652

Invalid length for a Base-64 char array or string. when I trying to pass byte[]

I am trying send Byte[] by converting to string from View to controller in @Html.ActionLink. Evey time when I click on ActionLink it is throwing exception. I am attaching code here.

Exception

enter image description here

URL After Action Click

  http://localhost:55253/Member/Create?customerContactNumber=0439349
&committeeId=AAAAAAAADLc%3D

View Code

    @using VolunteerPoints.BootstrapSupport
    @model Tuple<VolunteerPoints.Models.Contact, IEnumerable<VolunteerPoints.Data.Committee>>

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

    <h2>Activity Search Results</h2>
    <table id="Activitieslist" class="table table-striped table-bordered table-hover .table-condensed">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Item2.GetEnumerator().Current.Committee_Name)
                </th>

                <th>
                    @Html.DisplayNameFor(model => model.Item2.GetEnumerator().Current.Committee_Type)
                </th>

                <th></th>
            </tr>
        </thead>
        @foreach (var model in Model.Item2)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => model.Committee_Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => model.Committee_Type)
                </td>

                <td>
                    <div>
                        <div>
                            @Html.ActionLink("Select", "Create","Member", new 

{customerContactNumber = Model.Item1.Number, committeeId = 
Convert.ToBase64String(model.Committee_Id) }, new { @class = "btn btn-primary" })
                        </div>
                    </div>
                </td>
            </tr>
        }

    </table>

    @section Scripts {
              @Styles.Render("~/Content/DataTables/css")
            @Scripts.Render("~/bundles/DataTables") 

        <script type="text/JavaScript">
            $(document).ready(function () {

                $('#Activitieslist').dataTable({
                    "bSort": true,
                    "bPaginate": false,
                    "bAutoWidth": false,
                });

            });
        </script>
    }

Upvotes: 1

Views: 1459

Answers (2)

Catto
Catto

Reputation: 6409

Try changing the section of code:

committeeId = Convert.ToBase64String(model.Committee_Id)

To

committeeId = HttpServerUtility.UrlTokenEncode(model.Committee_Id)

This will provide a more URL friendly encrypted string which will avoid characters in the URL that may cause errors.

Hope this helps you James123;

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500505

Well this part of the URL:

AAAAAAAADLc%3D

should be decoded to

AAAAAAAADLc=

... at which point the length is a multiple of 4, with perfectly reasonable padding at the end.

So I suspect the problem is how/whether the decoding is performed.

(As a side note: byte[] is a pretty unusual representation for an ID. Do you really need it to be done that way?)

Upvotes: 2

Related Questions