1

So I have the following route in my RouteConfig.cs

routes.MapRoute(
    name: "Student",
    url: "student",
    defaults: new { controller = "Home", action = "Student", id = UrlParameter.Optional }
);

Based on this route, I have the following method in my Home controller:

public ActionResult Student()
{
    return View();
}

This simple route when invoked http://localhost:54326/student will take me to the Student view. All good till now.

How can I achieve this route: http://localhost:54326/student/01-28-2021 when I invoke the above route automatically?

Basically, I want to append a string at the end of the original route when it is invoked.

Is there anything I can specify in RouteConfig through which I can achieve this?

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • why you do not simply use the date as parameter? how many dates you gone make route of? – Maytham Fahmi Jan 29 '21 at 22:36
  • I do not want to type the date string. I want the current date to automatically be appended as a string to the url when the original url is invoked. I tried an internal redirect but it is not working. – Rahul Sharma Jan 29 '21 at 22:40
  • Why not return a redirect? – Caius Jard Jan 29 '21 at 23:10
  • @CaiusJard I did try that but if I go that way, I get a URL like this: `http://localhost:54326/Home/Student?date=01-30-2021` .I am looking for a URL like: `http://localhost:54326/student/01-28-2021` – Rahul Sharma Jan 30 '21 at 11:24
  • 1
    A perfectly valid question, but I would suggest using the date format `2021-01-28` for 2 reasons 1) it is a format that is recognized anywhere in the world 2) it will properly sort chronologically when added to a list (for example, browser history). – NightOwl888 Jan 31 '21 at 06:40

1 Answers1

1

The following route Student will allow to append a string at the end of http://localhost:54326/student when it is invoked.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// It is important that you add this route before the `Default` one.
// Routes are processed in the order they are listed, 
// and we need the new route to take precedence over the default.

routes.MapRoute(
    name: "Student",
    url: "Student/{date}",
    defaults: new { controller = "Home", action = "Student", date = UrlParameter.Optional}
    );

The Student action declaration:

public ActionResult Student(string date)
{
    //string dateFormat = "MM-dd-yyyy";
    string dateFormat = "dd-MM-yyyy";

    if (string.IsNullOrEmpty(date))
    {            
        return RedirectToAction("Student", new { date = DateTime.Now.ToString(dateFormat) });
    }
    else if (!DateTime.TryParseExact(date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dt))
    {
        return RedirectToAction("ReportErrorFormat");
    }
    return View((object)date);
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Thank you very much for the answer but unfortunately it did not work. The current date is still not being appended to the URL. – Rahul Sharma Jan 30 '21 at 11:08
  • @Rahul Sharma: I have changed the `Student` action method to call the `RedirectToActionPermanent()`. Now if `http://localhost:54326/student` is entered it will return `http://localhost:54326/student/01-30-2021` URL. – Jackdaw Jan 30 '21 at 14:14
  • How can I change the date format in the URL from `http://localhost:54326/student/01-30-2021` to `http://localhost:54326/student/30-01-2021` ? – Rahul Sharma Jan 30 '21 at 17:13
  • @Rahul Sharma: Some fixes are made in the `Student()` action method to support `dd-MM-yyyy` date format. Select proper the `dateFormat` pattern depend on required date format. Another approach is to change all date-time functions to use current regional settings on the server. – Jackdaw Jan 30 '21 at 22:54
  • @Rahul Sharma: Some helpful tips for dealing with `DateTime` you can read here: [https://stackoverflow.com/q/4331189/6630084](https://stackoverflow.com/q/4331189/6630084) – Jackdaw Jan 30 '21 at 23:17
  • Now, nothing is working. It is saying localhost redirected you too many times. Also I keep getting the same value in `date` variable which is: `01-30-2021` and my final URL no matter what I try ends up:`http://localhost:54326/student/01-30-2021`. It seems that in `public ActionResult Student(string date)` the same value is being passed and I am not sure how is that happening since I do not give any value in the URL. – Rahul Sharma Jan 31 '21 at 09:08
  • @Rahul Sharma: Try to remove `obj` and `bin` folders. Recompile projects. The IIS is blocking your files from updating. If you will see message that some files in the `bin` folder is locking this is the reason why `nothing is working`. – Jackdaw Jan 31 '21 at 09:12
  • Cleared the cache and tried again. It is working as expected. Thanks again. – Rahul Sharma Jan 31 '21 at 10:10
  • @Rahul Sharma: Thank you for feedback. But it looks like there are dark places in the MVC. :) – Jackdaw Jan 31 '21 at 10:12