This question is usually asked in the context of dissection for educational rather than medical or research purposes, so that’s the context in which I’ll try to answer this one. Dissection unfortunately remains extremely common on biology, zoology and veterinary science courses, among others.
Starting with ethics, it is first important to acknowledge that there is really nothing that is learned through dissection which can’t be learned elsewhere. Many education institutions are already moving away from dissections, less for ethical issues and more due to the fact that it isn’t thought by many to be a particularly useful teaching method. Interactive software, video observations and textbooks can teach many of the same things without requiring the death of an animal. These methods are more modern, far more humane and if the same learning outcomes can be achieved by using these alternative methods then there is no real reason to make use of dissection for the vast majority of cases in which it is used. Those which are essential for the subject matter being taught should be sourced solely from voluntarily surrendered animals who have been humanely euthanised due to mortal, incurable illness or injury.
It is a common myth that dissection animals are essentially “byproducts” of other industries, but this is not the case. Many mice and rats, are domestically bred, raised and sold solely for the purposes of dissection, while other animals such as frogs, salamanders, birds, snakes, turtles, fish, and most other invertebrate animals used in dissection are predominantly taken from the wild. These numbers are not small either, in the US alone, around 10 million vertebrates and 10 million invertebrates are used per year. When organs are dissected, these are usually sourced from slaughterhouses; meaning that their use and sale helps keep the rearing and slaughtering of animals a profitable concern. Dealers who supply these animals often stockpile animals on top of one another and ship them in crowded containers with no temperature regulation, food, or water. Undercover video footage has exposed that some are still alive as they are pumped full of formaldehyde or other preservatives.
In terms of whether or not you have to do it, that really depends on a number of factors. Some states in the US have enacted laws protecting a student’s right to choose a humane alternative, including Florida, California, Connecticut Pennsylvania, New York, Rhode Island, Illinois, Virginia, Oregon, New Jersey and Vermont. Maine, Maryland, Massachusetts, and New Mexico and Louisiana. Some countries like Argentina, Israel and Slovakia have banned the practice from schools entirely, whereas Italy, Queensland Australia and India all uphold the right for conscientious objectors to refuse to take part in dissection. If you live in any one of these places, cite the law on this and simply refuse, they will not be able to punish you in any way so long as you are willing to take part in a humane alternative, most of which will be book or software based.
If you do not belong to any of these countries or states, there are still options available to you. Your first step should just be to discuss it with your teacher or professor, explain the fact that you are morally opposed to dissection for education purposes, and that you would like to instead opt for a humane alternative. The key is to make it clear that you are not being lazy, offer to do a written or research based assignment instead, or to write a report or an essay on a dissection video if necessary. You need to be careful to be very polite here, tell them you understand why it is done, but your beliefs will not allow you to take part in something like this. Don’t get bogged down arguing about the ethics, be firm and simply state that regardless of what their arguments are, where they say the animals come from, you do not believe dissection is ethical.
If they still refuse, your next steps will depend largely on the situation. If you are a minor, you should ask your teacher if they would be willing to meet with your parents to discuss it further. This is worth trying even if you know your parents won’t attend a meeting, as most of the time this offer by itself will be enough to make your teacher think twice, as they don’t want to deal with angry parents and lose their free time on an extended meeting. If your teacher still refuses, try to obtain a note from home or actually arrange that meeting if your parents are supportive; very few schools would go against the wishes of your parents or guardian over such a sensitive issue. If this still doesn’t work, you can still refuse and simply take whatever the punishment is, if that is an option for you.
If you are not a minor and are in a university context, then you could try appealing to the head of the department, in writing. This will often be enough to show how serious this is for you, and a compromise can be discussed with them. If this doesn’t work, you could offer to observe and take notes as a last resort. Failing that, you need to look at whether or not the exercise counts towards your grade; if it doesn’t you may simply refuse to take part and take the punishment, or just call in sick on that day, even though you and your professor will both know why you did it.
If you have done all you practically can to fight it and you do not have the option to push any further and refuse, then it may be something you are forced to take part in. This is not your fault, and it is okay to compromise on this in order to obtain a qualification. If you are doing an animal based degree, which dissections will usually be required on, going through this could give you a qualification which can help you do some real good in the world, and these fields could certainly benefit from more animal rights advocates pushing for change, including on the practice of dissection. It will not usually come to that, but if it does then keep in mind we can only avoid exploitation as far as is practicable, and no one can reasonably expect you to forfeit your qualification due the ethical issues involved in dissection.