Subrato
Subrato

Reputation: 15

How to block access to controller via changing Url in ASP.NET Core?

I want to access page when user clicks to button.

By changing url, such as ..../Member/Batch/Create I want my site redirect user to another page.

How can I do this via ASP.NET Core MVC 3.1?

Upvotes: 0

Views: 625

Answers (2)

Brando Zhang
Brando Zhang

Reputation: 28432

Do you mean we could only access the controller by using button?

If this is your requirement, you could try to check the Request.Headers["Referer"].ToString() to make sure just which url could access this controller.

Like below:

    public IActionResult Privacy()
    {
        var re = Request.Headers["Referer"].ToString();
        if (Request.Headers["Referer"].ToString() != "https://localhost:44342/home/index")
        {
            return View("Error");
        }
        else
        {
            return View();
        }

    }

Result:

enter image description here

Upvotes: 0

Lovely
Lovely

Reputation: 316

Use:

<a href="@Url.Action("Batch", "Member", null)">Create Batch</a>

See this answer and help for more details.

Upvotes: 1

Related Questions