1

I am getting this error: A potentially dangerous Request.Path value was detected from the client (:).

I am trying to pass a DateTime and an int to an ActionResult, I have created a custom route to deal with it. I can't see (:) in my URL so don't know how to replace the string. Is it the (%) in the date of my URL that are causing it? Here is my code that is passing the variables to the controller:

//View
DateTime raceDate = DateTime.Today;
<center><a href="@Url.Action("EventInfo", new {raceDate= date, trackID=trID})" class="buttonize" target="_blank">View</a></center>

//Controller
public ActionResult EventInfo(DateTime raceDate, int trackID )
    {
        EventInfomodel = new EventInfo();
        model.MyRace = db.GetRaceDetails(raceDate, trackID)
        return View(model);
    }

//Route.Config
routes.MapRoute("Home/EventInfo", "Home/EventInfo/{raceDate}/{trackID}", new { controller = "Home", action = "EventInfo", raceDate = UrlParameter.Optional, trackID= UrlParameter.Optional });

The URL in my browser looks like this:

 Home/EventInfo/04/22/2019%2000%3a00%3a00/59

I'm completely new MVC and C# so any help would be great, thank you.

1
  • 2
    You have two parameters, Home/EventInfo/{raceDate}/{trackID} race date and track id. The url in your browser is : Home/EventInfo/04/22/2019%2000%3a00%3a00/59 Your raceDate is being passes as 04/22/2019, in which the / is considered in the path which wont be existing. Try formatting your date and then try Apr 22, 2019 at 9:57

2 Answers 2

1

Security is reason because of that you getting error

Why : URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers.

Solution : When you compose your URL need to be encoded in format .

HttpContext.Current.Server.UrlEncode(<Your URL>);

Thank You.

1
  • And Also your Date format Contains "/" characters , so that the MVC routing use "/" character to define route in URL ..Home/EventInfo/04/22/2019 ....not such path exists in your route table Apr 22, 2019 at 10:23
0

@Praneet Nadkar Your suggestion has worked, I formatted the date to a string in my view like so:

DateTime mydate = DateTime.Today;
string   date   = mydate.ToString("yyyy-MM-dd");

Now my URL looks much better and works

Home/EventInfo/2019-04-22/59

Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.