简体   繁体   中英

ASP.NET MVC - Regex, Error: Parameter can not be null. Parameter Name: input

In the first line of the Tuple method, in the expression Regex.Match ; Parameter can not be null, Parameter name: input gives the error. I tried, but I could not solve the problem. How do I solve it?

    public ActionResult Show(string idAndSlug)
    {
        var parts = SeperateIdAndSlug(idAndSlug);
        if (parts == null)
            return HttpNotFound();

        var post = Database.Session.Load<Post>(parts.Item1);
        if (post == null || post.IsDeleted)
            return HttpNotFound();

        if (!post.Slug.Equals(parts.Item2, StringComparison.CurrentCultureIgnoreCase))
            return RedirectToRoutePermanent("Post", new { id = parts.Item1, slug = post.Slug });

        return View(new PostsShow
        {
            Post = post
        });
    }

    private Tuple<int, string> SeperateIdAndSlug(string idAndSlug)
    {
        var matches = Regex.Match(idAndSlug, @"^(\d+)\-(.*)?$");
        if (!matches.Success)
            return null;
        var id = int.Parse(matches.Result("$1"));
        var slug = matches.Result("$2");
        return Tuple.Create(id, slug);
    }

确保den Show Action中的参数idAndSlug不为null,否则如果为null则将不起作用。

Just ensure the parameter is not null.

public ActionResult Show(string idAndSlug)
{
    if(idAndSlug == null)
        throw new ArgumentNullException(nameof(idAndSlug));
    //your code
}

or

public ActionResult Show(string idAndSlug)
{
    if(idAndSlug == null)
        idAndSlug = "";
    //your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM